home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gsview13 / src / gvwinit.c < prev    next >
C/C++ Source or Header  |  1995-12-09  |  15KB  |  465 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. /* gvwinit.c */
  19. /* Initialisation routings for Windows GSview */
  20. #include "gvwin.h"
  21.  
  22. /* Open/Save File Dialog Box */
  23. OPENFILENAME ofn;
  24. char szOFilename[MAXSTR];    /* filename for OFN */
  25. char szOFilter[256];        /* filter for OFN */
  26. /* buttons */
  27. WNDPROC lpfnButtonWndProc;    /* default button WndProc */
  28. struct buttonlist {
  29.    HWND hbutton;
  30.    struct buttonlist *next;
  31. };
  32. struct buttonlist *buttonhead, *buttontail;
  33. int real_button_width;
  34.  
  35. /* Don't start another instance - use previous instance */
  36. void
  37. gsview_init0(LPSTR lpszCmdLine)
  38. {
  39.     HWND hwnd = FindWindow(szClassName, szAppName);
  40.     BringWindowToTop(hwnd);
  41. #if __BORLANDC__ == 0x452
  42.     /* avoid bug in BC++ 4.0 */
  43. #ifdef __WIN32__
  44.     /* skip over EXE name */
  45.     while ( *lpszCmdLine && (*lpszCmdLine!=' ')) 
  46.         lpszCmdLine++;
  47.     while ( *lpszCmdLine && (*lpszCmdLine==' ')) 
  48.         lpszCmdLine++;
  49. #endif
  50. #endif
  51.     if (lstrlen(lpszCmdLine) != 0) {
  52.         /* open file specified on command line */
  53.         HGLOBAL hglobal;
  54.         LPSTR szFile;
  55.         hglobal = GlobalAlloc(GHND | GMEM_SHARE, lstrlen(lpszCmdLine)+1);
  56.         if (hglobal) {
  57.             szFile = GlobalLock(hglobal);
  58.         lstrcpy(szFile, lpszCmdLine);
  59.             GlobalUnlock(hglobal);
  60.         PostMessage(hwnd, WM_COMMAND, IDM_DROP, (LPARAM)hglobal);
  61.         }
  62.     }
  63. }
  64.  
  65. /* main initialisation */
  66. void
  67. gsview_init1(LPSTR lpszCmdLine)
  68. {
  69. WNDCLASS wndclass;
  70. DWORD version = GetVersion();
  71. char *p;
  72. char workdir[MAXSTR];
  73. char filedir[MAXSTR];
  74. int length = 64;
  75.  
  76.     while (length && !SetMessageQueue(length))
  77.         length--;    /* reduce size and try again */
  78.     if (length == 0)
  79.         exit(0);    /* panic */
  80.     
  81.     /* figure out which version of Windows */
  82.     if ((LOBYTE(LOWORD(version))<<8) + HIBYTE(LOWORD(version)) >= 0x30a)
  83.         is_win31 = TRUE;
  84. #ifdef __WIN32__
  85.     /* check if Windows NT */
  86.     if ((HIWORD(version) & 0x8000)==0)
  87.         is_winnt = TRUE;
  88.     /* check if Windows 95 (Windows 4.0) */
  89.     if ((LOBYTE(LOWORD(version))<<8) + HIBYTE(LOWORD(version)) >= 0x400) {
  90.         is_win95 = TRUE;
  91.     }
  92. #endif
  93.  
  94.     /* get path to EXE */
  95.     GetModuleFileName(phInstance, szExePath, sizeof(szExePath));
  96.     if ((p = strrchr(szExePath,'\\')) != (char *)NULL)
  97.         p++;
  98.     else
  99.         p = szExePath;
  100.     *p = '\0';
  101.  
  102.     /* get path to INI file */
  103.     szIniFile[0] = '\0';
  104.     /* strcpy(szIniFile, szExePath); */
  105.     strcat(szIniFile, INIFILE);
  106.  
  107.     /* get path to help file */
  108.     strcpy(szHelpName, szExePath);
  109.     p = szHelpName + strlen(szHelpName);
  110.     LoadString(phInstance, IDS_HELPFILE, p, sizeof(szHelpName) - (p-szHelpName));
  111.  
  112.     /* help message for GetOpenFileName Dialog Box */
  113.     help_message = RegisterWindowMessage(HELPMSGSTRING);
  114.     LoadString(phInstance, IDS_TOPICROOT, szHelpTopic, sizeof(szHelpTopic));
  115.  
  116.         load_string(IDS_WAIT, szWait, sizeof(szWait));    /* generic wait message */
  117.     
  118.     /* register the window class */
  119.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  120.     wndclass.lpfnWndProc = WndImgProc;
  121.     wndclass.cbClsExtra = 0;
  122.     wndclass.cbWndExtra = sizeof(LONG);
  123.     wndclass.hInstance = phInstance;
  124.     wndclass.hIcon = LoadIcon(phInstance,MAKEINTRESOURCE(ID_GSVIEW));
  125.     wndclass.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
  126.     wndclass.hbrBackground =  GetStockObject(WHITE_BRUSH);
  127.     wndclass.lpszMenuName = NULL;
  128.     wndclass.lpszClassName = szClassName;
  129.     RegisterClass(&wndclass);
  130.  
  131.     strcpy(option.gscommand, szExePath);
  132.     strcat(option.gscommand, DEFAULT_GSCOMMAND);
  133.     strcat(option.gscommand, " -I");
  134.     strcat(option.gscommand, szExePath);
  135.     strcpy(option.gscommand+strlen(option.gscommand)-1, ";");
  136.     strcat(option.gscommand, szExePath);
  137.     strcat(option.gscommand, "fonts;");
  138.     strcat(option.gscommand, "c:\\psfonts");
  139.     option.img_origin.x = CW_USEDEFAULT;
  140.     option.img_origin.y = CW_USEDEFAULT;
  141.     option.img_size.x = CW_USEDEFAULT;
  142.     option.img_size.y = CW_USEDEFAULT;
  143.     option.gsversion = IDM_GS3;
  144.     option.unit = IDM_UNITPT;
  145.     option.quick = TRUE;
  146.     option.settings = TRUE;
  147.     option.button_show = TRUE;
  148.     option.fit_page = TRUE;
  149.     option.safer = TRUE;
  150.     option.media = IDM_LETTER;
  151.     strcpy(option.medianame, "letter");
  152.     option.user_width = 610;
  153.     option.user_height = 792;
  154.     option.epsf_clip = FALSE;
  155.     option.epsf_warn = FALSE;
  156.     option.ignore_dsc = FALSE;
  157.     option.show_bbox = FALSE;
  158.     option.redisplay = TRUE;
  159.     option.orientation = IDM_PORTRAIT;
  160.     option.swap_landscape = FALSE;
  161.     option.xdpi = DEFAULT_RESOLUTION;
  162.     option.ydpi = DEFAULT_RESOLUTION;
  163.     option.save_dir = TRUE;
  164.     /* defaults if entry not in gsview.ini */
  165.     hmenu = LoadMenu(phInstance, "gsview_menu");
  166.     haccel = LoadAccelerators(phInstance, "gsview_accel");
  167.     getcwd(workdir, sizeof(workdir));
  168.     /* read entries from gsview.ini */
  169.     read_profile();
  170.  
  171.     hwndimg = CreateWindow(szClassName, (LPSTR)szAppName,
  172.           WS_OVERLAPPEDWINDOW,
  173.           option.img_origin.x, option.img_origin.y, 
  174.           option.img_size.x, option.img_size.y, 
  175.           NULL, NULL, phInstance, (void FAR *)NULL);
  176.  
  177.     /* load DLL for sounds */
  178.     if (is_win31) {
  179.         /* MMSYSTEM.DLL requires Windows 3.1, so to allow gsview to run
  180.            under Windows 3.0 we can't use the import library */
  181. #ifdef __WIN32__
  182.         hlib_mmsystem = LoadLibrary("WINMM.DLL");
  183.         if (hlib_mmsystem != NULL) {
  184.         lpfnSndPlaySound = (FPSPS)GetProcAddress(hlib_mmsystem, "sndPlaySoundA");
  185.         }
  186. #else
  187.         hlib_mmsystem = LoadLibrary("MMSYSTEM.DLL");
  188.         if (hlib_mmsystem >= HINSTANCE_ERROR) {
  189.         lpfnSndPlaySound = (FPSPS)GetProcAddress(hlib_mmsystem, "sndPlaySound");
  190.         }
  191. #endif
  192.         else {
  193.         gserror(IDS_SOUNDNOMM, NULL, MB_ICONEXCLAMATION, -1);
  194.         hlib_mmsystem = (HINSTANCE)NULL;
  195.         }
  196.     }
  197.  
  198. #if __BORLANDC__ == 0x452
  199.     /* avoid bug in BC++ 4.0 */
  200. #ifdef __WIN32__
  201.     /* skip over EXE name */
  202.     while ( *lpszCmdLine && (*lpszCmdLine!=' ')) 
  203.         lpszCmdLine++;
  204.     while ( *lpszCmdLine && (*lpszCmdLine==' ')) 
  205.         lpszCmdLine++;
  206. #endif
  207. #endif
  208.  
  209.     if (lstrlen(lpszCmdLine) >= 2) {
  210.         if ( ((lpszCmdLine[0] == '/') || (lpszCmdLine[0] == '-'))
  211.          &&  ((lpszCmdLine[1] == 'D') || (lpszCmdLine[1] == 'd')) ) {
  212.         debug = TRUE;
  213.         lpszCmdLine += 2;
  214.         while (*lpszCmdLine && (*lpszCmdLine == ' '))
  215.             lpszCmdLine++;
  216.         }
  217.      }
  218.     if (lstrlen(lpszCmdLine) != 0) {
  219.         /* open file specified on command line */
  220.         HGLOBAL hglobal;
  221.         LPSTR szFile;
  222.         /* deal with filenames in quotes */
  223.         /* quotes within filenames are not permitted */
  224.         if (*lpszCmdLine == '\042') {
  225.         LPSTR p;
  226.         lpszCmdLine++;    /* skip over first quote */
  227.         p = lpszCmdLine;
  228.         while (*p && (*p != '\042'))
  229.             p++;
  230.         *p = '\0';    /* remove end quote */
  231.         }
  232.         hglobal = GlobalAlloc(GHND | GMEM_SHARE, lstrlen(lpszCmdLine)+1);
  233.         if (hglobal) {
  234.             szFile = GlobalLock(hglobal);
  235.         lstrcpy(szFile, lpszCmdLine);
  236.             GlobalUnlock(hglobal);
  237.         PostMessage(hwndimg, WM_COMMAND, IDM_DROP, (LPARAM)hglobal);
  238.         }
  239.         /* ignore last saved directory */
  240.         /* use directory of file if given, or work directory */
  241.         if ((lpszCmdLine[0] == '/') || (lpszCmdLine[0] == '-')) {
  242.         lpszCmdLine += 2;
  243.         while (*lpszCmdLine && (*lpszCmdLine == ' '))
  244.             lpszCmdLine++;
  245.         }
  246.         lstrcpy(filedir, lpszCmdLine);
  247.         if ( (p = strrchr(filedir, '\\')) == (char *)NULL ) {
  248.             if ( (p = strrchr(filedir, ':')) == (char *)NULL )
  249.             strcpy(filedir, workdir);  /* no path so use work directory */
  250.         else
  251.             *(++p) = '\0';
  252.         }
  253.         else
  254.         *(++p) = '\0';
  255.         if (isalpha(filedir[0]) && (filedir[1]==':'))
  256.         (void) setdisk(toupper(filedir[0])-'A');
  257.         if (!((strlen(filedir)==2) && isalpha(filedir[0]) && (filedir[1]==':')))
  258.             chdir(filedir);
  259.     }
  260.     play_sound(SOUND_START);
  261.         if (changed_version) {
  262.         message_box("The installed version of GSview has changed.  \
  263. Please read the Installation help and then correctly set\r\
  264. Options | Ghostscript Command", 0);
  265.         load_string(IDS_TOPICINSTALL, szHelpTopic, sizeof(szHelpTopic));
  266.         get_help();
  267.         }
  268. }
  269.  
  270. /* create gsview window menu bar and buttons */
  271. void
  272. gsview_create()
  273. {
  274. int i;
  275. char cReplace;
  276. WNDCLASS wndclass;
  277. HGLOBAL hglobal;
  278. short FAR *pButtonID;
  279. TEXTMETRIC tm;
  280. HDC hdc;
  281. HWND hbutton;
  282. WNDPROC    lpfnMenuButtonProc;
  283. POINT char_size;        /* size of default text characters */
  284. POINT button_size, button_shift;
  285. char thismedia[20];
  286. LOGFONT lf;
  287. HFONT old_hfont;
  288.  
  289.     /* setup OPENFILENAME struct */
  290.     if (!LoadString(phInstance, IDS_FILTER, szOFilter, sizeof(szOFilter)-1))
  291.         return;
  292.     cReplace = szOFilter[strlen(szOFilter)-1];
  293.     for (i=0; szOFilter[i] != '\0'; i++)
  294.         if (szOFilter[i] == cReplace)
  295.         szOFilter[i] = '\0';
  296.     ofn.lStructSize = sizeof(OPENFILENAME);
  297.     ofn.hwndOwner = hwndimg;
  298.     ofn.lpstrFilter = szOFilter;
  299.     ofn.nFilterIndex = FILTER_PS;
  300.     ofn.lpstrFile = szOFilename;
  301.     ofn.nMaxFile = sizeof(szOFilename);
  302.     ofn.lpstrFileTitle = (LPSTR)NULL;
  303.     ofn.nMaxFileTitle = 0;
  304.     ofn.lpstrTitle = (LPSTR)NULL;
  305.     ofn.lpstrInitialDir = (LPSTR)NULL;
  306.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_SHOWHELP;
  307.     LoadString(phInstance, IDS_TOPICROOT, szHelpTopic, sizeof(szHelpTopic));
  308.  
  309.     /* add menu to image window */
  310.     SetMenu(hwndimg, hmenu);
  311.  
  312.     /* get default text size */
  313.     hdc = GetDC(hwndimg);
  314.     memset(&lf, 0, sizeof(LOGFONT));
  315.     lf.lfHeight = 8;  /* 8 pts */
  316.     strcpy(lf.lfFaceName, "Helv");
  317.     info_font = CreateFontIndirect(&lf);
  318.     old_hfont = SelectObject(hdc, info_font);
  319.     GetTextMetrics(hdc,(LPTEXTMETRIC)&tm);
  320.     display.planes = GetDeviceCaps(hdc, PLANES);
  321.     display.bitcount = GetDeviceCaps(hdc, BITSPIXEL);
  322.     SelectObject(hdc, old_hfont);
  323.     ReleaseDC(hwndimg,hdc);
  324.     char_size.x = tm.tmAveCharWidth;
  325.     char_size.y = tm.tmHeight;
  326.  
  327.     /* set size of info area, buttons and offset to child window */
  328.     info_rect.left = 0;
  329.     info_rect.right = info_rect.left + 64 * char_size.x;
  330.     info_rect.top = 0;
  331.     info_rect.bottom = char_size.y+4;
  332.     button_size.x = 24;
  333.     button_size.y = 24;
  334.     button_shift.x = 0;
  335.     button_shift.y = button_size.y - 1;
  336.     button_rect.top = info_rect.bottom;
  337.     button_rect.left = -1;
  338.     button_rect.right = button_size.x - 2;
  339.     button_rect.bottom = 0;        /* don't care */
  340.     real_button_width = button_rect.right;
  341.     if (!option.button_show)
  342.         button_rect.right = 0;
  343.     img_offset.x = button_rect.right + (option.button_show ? 1 : 0);
  344.     img_offset.y = info_rect.bottom + 1;
  345.     info_file.x = info_rect.left + 2;
  346.     info_file.y = 2;
  347.     info_coord.left = info_rect.left + 20 * char_size.x;
  348.     info_coord.right = info_rect.left + 34 * char_size.x;
  349.     info_coord.top = 2;
  350.     info_coord.bottom = char_size.y+4;
  351.     info_page.x = info_rect.left + 36 * char_size.x + 2;
  352.     info_page.y = 2;
  353.  
  354.     /* check menu items */
  355.     for (i=IDM_LETTER; i<IDM_USERSIZE; i++) {
  356.         get_menu_string(IDM_MEDIAMENU, i, thismedia, sizeof(thismedia));
  357.         if (!stricmp(thismedia, option.medianame)) {
  358.         break;
  359.         }
  360.     }
  361.     option.media = i;
  362.     strncpy(option.medianame,thismedia,sizeof(option.medianame));
  363.     CheckMenuItem(hmenu, option.unit, MF_BYCOMMAND | MF_CHECKED);
  364. #if !defined(__WIN32__) && defined(GS261)
  365.     check_menu_item(IDM_GSVERMENU, option.gsversion, TRUE);
  366. #endif
  367.     CheckMenuItem(hmenu, option.media, MF_BYCOMMAND | MF_CHECKED);
  368.     CheckMenuItem(hmenu, option.orientation, MF_BYCOMMAND | MF_CHECKED);
  369.     CheckMenuItem(hmenu, gsview_depth_to_menu(option.depth), MF_BYCOMMAND | MF_CHECKED);
  370.     if (option.epsf_clip)
  371.         CheckMenuItem(hmenu, IDM_EPSFCLIP, MF_BYCOMMAND | MF_CHECKED);
  372.     if (option.epsf_warn)
  373.         CheckMenuItem(hmenu, IDM_EPSFWARN, MF_BYCOMMAND | MF_CHECKED);
  374.     if (option.ignore_dsc)
  375.         CheckMenuItem(hmenu, IDM_IGNOREDSC, MF_BYCOMMAND | MF_CHECKED);
  376.     if (option.show_bbox)
  377.         CheckMenuItem(hmenu, IDM_SHOWBBOX, MF_BYCOMMAND | MF_CHECKED);
  378.     if (option.swap_landscape)
  379.         CheckMenuItem(hmenu, IDM_SWAPLANDSCAPE, MF_BYCOMMAND | MF_CHECKED);
  380.     if (option.save_dir) 
  381.         CheckMenuItem(hmenu, IDM_SAVEDIR, MF_BYCOMMAND | MF_CHECKED);
  382.     if (option.button_show) 
  383.         CheckMenuItem(hmenu, IDM_BUTTONSHOW, MF_BYCOMMAND | MF_CHECKED);
  384.     if (option.fit_page) 
  385.         CheckMenuItem(hmenu, IDM_FITPAGE, MF_BYCOMMAND | MF_CHECKED);
  386.     if (option.quick) 
  387.         CheckMenuItem(hmenu, IDM_QUICK, MF_BYCOMMAND | MF_CHECKED);
  388.     if (option.safer) 
  389.         CheckMenuItem(hmenu, IDM_SAFER, MF_BYCOMMAND | MF_CHECKED);
  390.     if (option.redisplay) 
  391.         CheckMenuItem(hmenu, IDM_AUTOREDISPLAY, MF_BYCOMMAND | MF_CHECKED);
  392.     if (option.settings)
  393.         CheckMenuItem(hmenu, IDM_SAVESETTINGS, MF_BYCOMMAND | MF_CHECKED);
  394.  
  395.     hcWait = LoadCursor((HINSTANCE)NULL, IDC_WAIT);
  396.  
  397.     /* add buttons */
  398.     lpfnMenuButtonProc = (WNDPROC)MakeProcInstance((FARPROC)MenuButtonProc, phInstance);
  399.     GetClassInfo((HINSTANCE)NULL, "button", &wndclass);    /* get default button class info */
  400.     lpfnButtonWndProc = wndclass.lpfnWndProc;
  401.     
  402.     hglobal = LoadResource(phInstance, FindResource(phInstance, MAKEINTRESOURCE(IDR_BUTTON), RT_RCDATA));
  403.     if ( (pButtonID = (short FAR *)LockResource(hglobal)) == (short FAR *)NULL)
  404.         return;
  405.     
  406.     for (i=0; pButtonID[i]; i++) {
  407.         hbutton = CreateWindow("button", NULL,
  408.             WS_CHILD | (option.button_show ? WS_VISIBLE : 0) | BS_OWNERDRAW,
  409.             button_rect.left + i * button_shift.x,
  410.             button_rect.top  + i * button_shift.y,
  411.             button_size.x, button_size.y,
  412.             hwndimg, (HMENU)pButtonID[i],
  413.             phInstance, NULL);
  414.         SetWindowLong(hbutton, GWL_WNDPROC, (LONG)lpfnMenuButtonProc);
  415.         if (hbutton) {
  416.         if (buttonhead == (struct buttonlist *)NULL)
  417.             buttontail = buttonhead = (struct buttonlist *)malloc(sizeof(struct buttonlist));
  418.         else {
  419.             buttontail->next = (struct buttonlist *)malloc(sizeof(struct buttonlist)); 
  420.             buttontail = buttontail->next;
  421.         }
  422.         buttontail->hbutton = hbutton;
  423.         buttontail->next = NULL;
  424.         }
  425.     }
  426.     FreeResource(hglobal);
  427. }
  428.  
  429. void
  430. show_buttons(void)
  431. {
  432. struct buttonlist *bp = buttonhead;
  433. RECT rect;
  434.     button_rect.right = option.button_show ? real_button_width : 0;
  435.     img_offset.x = button_rect.right + (option.button_show ? 1 : 0);
  436.     if (!option.button_show) {
  437.         while (bp) {
  438.             ShowWindow(bp->hbutton, SW_HIDE);
  439.             bp = bp->next;
  440.         }
  441.         if (hwndimgchild == (HWND)NULL) {
  442.             GetClientRect(hwndimg, &rect);
  443.             rect.right = real_button_width + 1;
  444.             rect.top = button_rect.top;
  445.             InvalidateRect(hwndimg, &rect, TRUE);
  446.             UpdateWindow(hwndimg);
  447.         }
  448.     }
  449.     GetClientRect(hwndimg, &rect);
  450.     SetWindowPos(hwndimgchild, (HWND)NULL, rect.left+img_offset.x, rect.top+img_offset.y,
  451.         rect.right-img_offset.x, rect.bottom-img_offset.y, 
  452.         SWP_NOZORDER | SWP_NOACTIVATE);
  453.     rect.right = real_button_width + 1;
  454.     rect.top = button_rect.top;
  455.     if (option.button_show) {
  456.         InvalidateRect(hwndimg, &rect, FALSE);
  457.         UpdateWindow(hwndimg);
  458.         while (bp) {
  459.             ShowWindow(bp->hbutton, SW_SHOWNA);
  460.             bp = bp->next;
  461.         }
  462.     }
  463. }
  464.  
  465.