Subversion Repositories filter_foundry

Rev

Rev 435 | Rev 444 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
  3.     Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
  4.     Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19. */
  20.  
  21. /* Win32 user interface routines */
  22.  
  23. #include "world.h"
  24.  
  25. #include "PIAbout.h"
  26.  
  27. #include <windows.h>
  28. #include <commctrl.h>
  29.  
  30. #include "ff.h"
  31. #include "slider_win.h"
  32. #include "version.h"
  33.  
  34. HWND preview_hwnd;
  35. HCURSOR hCurHandOpen;
  36. HCURSOR hCurHandGrab;
  37.  
  38. HCURSOR hCurHandQuestion;
  39. HICON hIconCautionSign;
  40.  
  41. extern HINSTANCE hDllInstance;
  42.  
  43. void DoAbout(AboutRecordPtr pb){
  44.         char text[1000];
  45.         char title[200];
  46.         PlatformData *p = (PlatformData*)pb->platformData;
  47.  
  48.         if (gdata && gdata->standalone) {
  49.                 sprintf(title, "About %s", gdata->parm.szTitle);
  50.                 sprintf(text,  "%s by %s\n" /* {Title} by {Author} */
  51.                                "%s\n" /* {Copyright} */
  52.                                "\n"
  53.                                "This plugin was built using Filter Foundry " VERSION_STR
  54.                                 #ifdef _WIN64
  55.                                " (64 bit)\n"
  56.                                 #else
  57.                                " (32 bit)\n"
  58.                                 #endif
  59.                                "(C) 2003-2009 Toby Thain, 2018-" RELEASE_YEAR " Daniel Marschall\n"
  60.                                "available from " PROJECT_URL,
  61.                                gdata->parm.szTitle,
  62.                                gdata->parm.szAuthor,
  63.                                gdata->parm.szCopyright);
  64.         } else {
  65.                 sprintf(title, "About Filter Foundry");
  66.                 sprintf(text,  "Filter Foundry " VERSION_STR
  67.                                 #ifdef _WIN64
  68.                                " (64 bit)\n"
  69.                                 #else
  70.                                " (32 bit)\n"
  71.                                 #endif
  72.                                "(C) 2003-2009 Toby Thain, 2018-" RELEASE_YEAR " Daniel Marschall\n"
  73.                                "\n"
  74.                                "Latest version available from\n"
  75.                                PROJECT_URL "\n"
  76.                                "\nPlease contact the author with any bug reports,\n"
  77.                                "suggestions or comments.\n"
  78.                                "If you use this program and like it, please consider\n"
  79.                                "making a donation.");
  80.         }
  81.  
  82.         MessageBox((HWND)p->hwnd, text, title, MB_TASKMODAL|MB_ICONINFORMATION|MB_OK);
  83. }
  84.  
  85. Boolean simplealert(char *s){
  86.         HWND hwnd;
  87.         char* title;
  88.         if (gdata && gdata->standalone) {
  89.                 title = gdata->parm.szTitle;
  90.         } else {
  91.                 title = _strdup("Filter Foundry");
  92.         }
  93.         hwnd = gdata ? gdata->hWndMainDlg : NULL;
  94.         return MessageBox(hwnd, s, title, MB_TASKMODAL|MB_ICONERROR|MB_OK) == IDOK;
  95. }
  96.  
  97. Boolean simplewarning(char* s) {
  98.         HWND hwnd;
  99.         char* title;
  100.         if (gdata && gdata->standalone) {
  101.                 title = gdata->parm.szTitle;
  102.         } else {
  103.                 title = _strdup("Filter Foundry");
  104.         }
  105.         hwnd = gdata ? gdata->hWndMainDlg : NULL;
  106.         return MessageBox(hwnd,s,title,MB_TASKMODAL|MB_ICONEXCLAMATION|MB_OK) == IDOK;
  107. }
  108.  
  109. Boolean showmessage(char *s) {
  110.         HWND hwnd;
  111.         char* title;
  112.         if (gdata && gdata->standalone) {
  113.                 title = gdata->parm.szTitle;
  114.         } else {
  115.                 title = _strdup("Filter Foundry");
  116.         }
  117.         hwnd = gdata ? gdata->hWndMainDlg : NULL;
  118.         return MessageBox(hwnd,s,title,MB_TASKMODAL|MB_ICONINFORMATION|MB_OK) == IDOK;
  119. }
  120.  
  121. INT_PTR CALLBACK maindlgproc(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam);
  122.  
  123. // Description:
  124. //   Creates a tooltip for an item in a dialog box.
  125. // Parameters:
  126. //   idTool - identifier of an dialog box item.
  127. //   nDlg - window handle of the dialog box.
  128. //   pszText - string to use as the tooltip text.
  129. // Returns:
  130. //   The handle to the tooltip.
  131. //
  132. HWND CreateToolTip(int toolID, HWND hDlg, PTSTR pszText) {
  133.         // Source: https://docs.microsoft.com/en-us/windows/win32/controls/create-a-tooltip-for-a-control (modified)
  134.  
  135.         HWND hwndTool, hwndTip;
  136.         TOOLINFO toolInfo;
  137.  
  138.         if (!toolID || !hDlg || !pszText) {
  139.                 return FALSE;
  140.         }
  141.         // Get the window of the tool.
  142.         hwndTool = GetDlgItem(hDlg, toolID);
  143.  
  144.         // Create the tooltip. g_hInst is the global instance handle.
  145.         hwndTip = CreateWindowEx(0, TOOLTIPS_CLASS, NULL,
  146.                 WS_POPUP | TTS_ALWAYSTIP /* | TTS_BALLOON*/,
  147.                 CW_USEDEFAULT, CW_USEDEFAULT,
  148.                 CW_USEDEFAULT, CW_USEDEFAULT,
  149.                 hDlg, NULL,
  150.                 hDllInstance, NULL);
  151.  
  152.         if (!hwndTool || !hwndTip) {
  153.                 return (HWND)NULL;
  154.         }
  155.  
  156.         // Associate the tooltip with the tool.
  157.         memset(&toolInfo, 0, sizeof(TOOLINFO)); // toolInfo = { 0 };
  158.         toolInfo.cbSize = sizeof(toolInfo);
  159.         toolInfo.hwnd = hDlg;
  160.         toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
  161.         toolInfo.uId = (UINT_PTR)hwndTool;
  162.         toolInfo.lpszText = pszText;
  163.         SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
  164.  
  165.         return hwndTip;
  166. }
  167.  
  168. INT_PTR CALLBACK maindlgproc(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam){
  169.         static POINT origpos;
  170.         static Point origscroll;
  171.         static Boolean panning = false;
  172.  
  173.         int item,i;
  174.         POINT newpos;
  175.         DRAWITEMSTRUCT *pdi;
  176.         Point newscroll;
  177.         HGDIOBJ hfnt;
  178.  
  179.         extern Boolean doupdates;
  180.         extern Handle preview_handle;
  181.  
  182.         if ((gdata->pluginDllSliderMessageId != 0) && (wMsg == gdata->pluginDllSliderMessageId)) {
  183.                 // This is for the PLUGIN.DLL sliders only
  184.                 if (doupdates) {
  185.                         int sliderNum = wParam - FIRSTCTLITEM;
  186.                         uint8_t sliderVal = (uint8_t)(lParam & 0xFFFF);
  187.                         slider[sliderNum] = sliderVal;
  188.  
  189.                         SETCTLTEXTINT(hDlg, FIRSTCTLTEXTITEM + sliderNum, sliderVal, false);
  190.                         REPAINTCTL(hDlg, FIRSTCTLTEXTITEM + sliderNum);
  191.  
  192.                         recalc_preview(gpb, hDlg);
  193.                 }
  194.                 return true;
  195.         }
  196.  
  197.         switch (wMsg) {
  198.         case WM_INITDIALOG:
  199.                 gdata->hWndMainDlg = hDlg;
  200.  
  201.                 if(gdata->standalone){
  202.                         SetWindowText(hDlg,gdata->parm.szTitle); // window title bar
  203.                 }
  204.                 centre_window(hDlg);
  205.  
  206.                 hfnt = GetStockObject(ANSI_FIXED_FONT);
  207.  
  208.                 hCurHandOpen = LoadCursor(hDllInstance, "HAND_OPEN");
  209.                 hCurHandGrab = LoadCursor(hDllInstance, "HAND_GRAB");
  210.                 hCurHandQuestion = LoadCursor(hDllInstance, "HAND_QUESTION");
  211.  
  212.                 hIconCautionSign = LoadIcon(hDllInstance, "CAUTION_ICO");
  213.  
  214.                 // Note: The whole class "Preview" gets the mouse cursor, not just the single item!
  215.                 preview_hwnd = GetDlgItem(hDlg, PREVIEWITEM);
  216.                 GetClientRect(preview_hwnd, &preview_rect);
  217.                 SetClassLongPtr(preview_hwnd, GCLP_HCURSOR, (LONG_PTR)hCurHandOpen);
  218.  
  219.                 // Note: The whole class "Caution" gets the mouse cursor, not just the single item!
  220.                 SetClassLongPtr(GetDlgItem(hDlg, FIRSTICONITEM), GCLP_HCURSOR, (LONG_PTR)hCurHandQuestion);
  221.  
  222.                 for(i = 0; i < 4; ++i){
  223.                         CreateToolTip(FIRSTICONITEM + i, hDlg, _strdup("Error in expression! Click to see details."));
  224.                 }
  225.  
  226.                 CreateToolTip(ZOOMINITEM, hDlg, _strdup("Zoom in"));
  227.                 CreateToolTip(ZOOMOUTITEM, hDlg, _strdup("Zoom out"));
  228.                 CreateToolTip(ZOOMLEVELITEM, hDlg, _strdup("Fully zoom in/out"));
  229.  
  230.                 for(i = 0; i < 8; ++i){
  231.                         if (gdata->pluginDllSliderMessageId == 0) {
  232.                                 // Non PLUGIN.DLL sliders
  233.                                 SetWindowLongPtr(GetDlgItem(hDlg, FIRSTCTLITEM + i), GWL_STYLE, TBS_HORZ | TBS_AUTOTICKS | WS_CHILD | WS_VISIBLE);
  234.                                 SendDlgItemMessage(hDlg, FIRSTCTLITEM + i, TBM_SETRANGE, TRUE, MAKELONG(0, 255));
  235.                                 SendDlgItemMessage(hDlg, FIRSTCTLITEM + i, TBM_SETTICFREQ, SLIDERPAGE, 0);
  236.                                 SendDlgItemMessage(hDlg, FIRSTCTLITEM + i, TBM_SETPAGESIZE, 0, SLIDERPAGE);
  237.                         }
  238.                         else {
  239.                                 // PLUGIN.DLL sliders
  240.                                 SetSliderRange(GetDlgItem(hDlg, FIRSTCTLITEM + i), 0, 255);
  241.                         }
  242.                         SendDlgItemMessage(hDlg,FIRSTCTLTEXTITEM+i,     EM_SETLIMITTEXT,3,0);
  243.                 }
  244.                 for(i = 0; i < 4; ++i){
  245.                         SendDlgItemMessage(hDlg,FIRSTEXPRITEM+i,        EM_SETLIMITTEXT,MAXEXPR-1,0); // we need 1 byte as NUL terminator, so our formula can be max 1023
  246.                         SendDlgItemMessage(hDlg,FIRSTEXPRITEM+i,        WM_SETFONT,(WPARAM)hfnt,false);
  247.                 }
  248.  
  249.                 maindlginit(hDlg);
  250.                 break;
  251.         case WM_DESTROY:
  252.                 gdata->hWndMainDlg = 0;
  253.                 DestroyCursor(hCurHandOpen);
  254.                 DestroyCursor(hCurHandGrab);
  255.                 DestroyCursor(hCurHandQuestion);
  256.                 DestroyIcon(hIconCautionSign);
  257.                 break;
  258.         case WM_DRAWITEM:
  259.                 pdi = (DRAWITEMSTRUCT*)lParam;
  260.                 if(pdi->itemAction == ODA_DRAWENTIRE){
  261.                         switch(pdi->CtlID){
  262.                         case PREVIEWITEM:
  263.                                 drawpreview(hDlg,pdi->hDC,PILOCKHANDLE(preview_handle,false));
  264.                                 PIUNLOCKHANDLE(preview_handle);
  265.                                 break;
  266.                         case FIRSTICONITEM:
  267.                         case FIRSTICONITEM + 1:
  268.                         case FIRSTICONITEM + 2:
  269.                         case FIRSTICONITEM + 3:
  270.                                 DrawIcon(pdi->hDC, 0, 0, hIconCautionSign);
  271.                                 break;
  272.                         default:
  273.                                 return false;
  274.                         }
  275.                 }else
  276.                         return false; // we couldn't handle the message
  277.                 break;
  278.         case WM_COMMAND:
  279.                 item = LOWORD(wParam);
  280.                 switch(HIWORD(wParam)){
  281.                 //case BN_CLICKED:
  282.                 case STN_CLICKED:
  283.                         // BN_CLICKED = Button clicked
  284.                         // STN_CLICKED = Static controls with SS_NOTIFY clicked
  285.                         // Both have the same ordinal number
  286.                         if(item==PREVIEWITEM && GetCursorPos(&origpos)){
  287.                                 panning = true;
  288.                                 origscroll = preview_scroll;
  289.                                 SetCursor(hCurHandGrab);
  290.                                 SetCapture(hDlg);
  291.                                 break;
  292.                         }
  293.                 /* ... falls through ... */
  294.                 case EN_CHANGE:
  295.                         if(doupdates && !maindlgitem(hDlg,item))
  296.                                 EndDialog(hDlg,item);
  297.                 }
  298.                 break;
  299. //      case WM_LBUTTONDOWN: break;
  300.         case WM_MOUSEMOVE:
  301.                 if(panning && GetCursorPos(&newpos)){
  302.                         newscroll.h = (int16)(origscroll.h - zoomfactor*((double)newpos.x - (double)origpos.x));
  303.                         newscroll.v = (int16)(origscroll.v - zoomfactor*((double)newpos.y - (double)origpos.y));
  304.                         if( newscroll.h != preview_scroll.h || newscroll.v != preview_scroll.v ){
  305.                                 preview_scroll = newscroll;
  306.                                 recalc_preview(gpb,hDlg);
  307.                         }
  308.                 }
  309.                 break;
  310.         case WM_LBUTTONUP:
  311.                 ReleaseCapture();
  312.                 panning = false;
  313.                 break;
  314.         case WM_HSCROLL:
  315.                 // Only for non-Plugin.dll-sliders
  316.                 item = GetDlgCtrlID((HWND)lParam);
  317.                 if(doupdates && gdata->pluginDllSliderMessageId == 0 && item>=FIRSTCTLITEM && item<=FIRSTCTLITEM+7)
  318.                         slidermoved(hDlg,item);
  319.                 break;
  320.         default:
  321.                 return false;
  322.         }
  323.  
  324.         return true;
  325. }
  326.  
  327. Boolean maindialog(FilterRecordPtr pb){
  328.         PlatformData *p;
  329.         INT_PTR res;
  330.  
  331.         // First try to use the sliders from PLUGIN.DLL (only Photoshop)
  332.         if (!Slider_Init_PluginDll("FoundrySlider")) {
  333.                 // If we couldn't get the sliders from PLUGIN.DLL (probably not running in Photoshop),
  334.                 // then try the Microsoft Trackbar Control instead
  335.                 if (!Slider_Init_MsTrackbar("FoundrySlider")) {
  336.                         // This will happen if we neither have PLUGIN.DLL, nor the Microsoft Trackbar Control (msctls_trackbar32).
  337.                         // "msctls_trackbar32" is not included in Windows NT 3.1, and since there is no OCX or RegSvr32.
  338.                         // It is included in Windows NT 3.5x.
  339.  
  340.                         //simplealert(_strdup("This plugin requires Photoshop's PLUGIN.DLL or the Microsoft Trackbar Control (msctls_trackbar32) which was not found on your system."));
  341.                         //return false;
  342.  
  343.                         // We simply hide the sliders and let the user enter the numeric values in the edit-box.
  344.                         simplewarning(_strdup("Visual sliders are not available because neither PLUGIN.DLL, nor the Microsoft Trackbar Control (msctls_trackbar32) was found on your system."));
  345.                         Slider_Init_None("FoundrySlider");
  346.                 }
  347.         }
  348.  
  349.         // For the preview image and caution symbols, we register subclasses, so that we can assign a mouse cursor to this class.
  350.         MakeSimpleSubclass("Preview", "STATIC");
  351.         MakeSimpleSubclass("Caution", "Button");
  352.  
  353.         // Now show the dialog
  354.         p = (PlatformData*)pb->platformData;
  355.  
  356.         // Note: "Invalid Cursor Handle" is the error when an unrecognized control class is detected
  357.         res = DialogBoxParam(hDllInstance,MAKEINTRESOURCE(gdata->standalone ? ID_PARAMDLG : ID_MAINDLG),
  358.                              (HWND)p->hwnd,maindlgproc,0);
  359.         if (res == 0) {
  360.                 simplealert(_strdup("DialogBoxParam in valid parent window handle"));
  361.         }
  362.         if (res == -1) {
  363.                 char s[100];
  364.                 strcpy(s, "DialogBoxParam failed: ");
  365.                 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, s + strlen(s), 0x100, NULL);
  366.                 dbg(s);
  367.         }
  368.  
  369.         // Clean up after the dialog has been closed
  370.         UnregisterClass("Preview", hDllInstance);
  371.         UnregisterClass("Caution", hDllInstance);
  372.         UnregisterClass("FoundrySlider", hDllInstance);
  373.         Slider_Uninit_PluginDll();
  374.  
  375.         return res == IDOK;
  376. }
  377.