Subversion Repositories filter_foundry

Rev

Rev 445 | Rev 454 | 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.         MessageBoxA((HWND)p->hwnd, text, title, MB_TASKMODAL|MB_ICONINFORMATION|MB_OK);
  83. }
  84.  
  85. Boolean simplealert(TCHAR* s){
  86.         HWND hwnd;
  87.         TCHAR title[256];
  88.         if (gdata && gdata->standalone) {
  89.                 #ifdef UNICODE
  90.                 mbstowcs(&title[0], (const char*)gdata->parm.szTitle, 256);
  91.                 #else
  92.                 strcpy(&title[0], gdata->parm.szTitle);
  93.                 #endif
  94.         }
  95.         else {
  96.                 xstrcpy(&title[0], (TCHAR*)TEXT("Filter Foundry"));
  97.         }
  98.         hwnd = gdata ? gdata->hWndMainDlg : NULL;
  99.         return MessageBox(hwnd, s, title, MB_TASKMODAL|MB_ICONERROR|MB_OK) == IDOK;
  100. }
  101.  
  102. Boolean simplewarning(TCHAR* s) {
  103.         HWND hwnd;
  104.         TCHAR title[256];
  105.         if (gdata && gdata->standalone) {
  106.                 #ifdef UNICODE
  107.                 mbstowcs(&title[0], (const char*)gdata->parm.szTitle, 256);
  108.                 #else
  109.                 strcpy(&title[0], gdata->parm.szTitle);
  110.                 #endif
  111.         } else {
  112.                 xstrcpy(&title[0], (TCHAR*)TEXT("Filter Foundry"));
  113.         }
  114.         hwnd = gdata ? gdata->hWndMainDlg : NULL;
  115.         return MessageBox(hwnd,s,title,MB_TASKMODAL|MB_ICONEXCLAMATION|MB_OK) == IDOK;
  116. }
  117.  
  118. Boolean showmessage(TCHAR *s) {
  119.         HWND hwnd;
  120.         TCHAR title[256];
  121.         if (gdata && gdata->standalone) {
  122.                 #ifdef UNICODE
  123.                 mbstowcs(&title[0], (const char*)gdata->parm.szTitle, 256);
  124.                 #else
  125.                 strcpy(&title[0], gdata->parm.szTitle);
  126.                 #endif
  127.         }
  128.         else {
  129.                 xstrcpy(&title[0], (TCHAR*)TEXT("Filter Foundry"));
  130.         }
  131.         hwnd = gdata ? gdata->hWndMainDlg : NULL;
  132.         return MessageBox(hwnd,s,title,MB_TASKMODAL|MB_ICONINFORMATION|MB_OK) == IDOK;
  133. }
  134.  
  135. INT_PTR CALLBACK maindlgproc(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam);
  136.  
  137. // Description:
  138. //   Creates a tooltip for an item in a dialog box.
  139. // Parameters:
  140. //   idTool - identifier of an dialog box item.
  141. //   nDlg - window handle of the dialog box.
  142. //   pszText - string to use as the tooltip text.
  143. // Returns:
  144. //   The handle to the tooltip.
  145. //
  146. HWND CreateToolTip(int toolID, HWND hDlg, LPTSTR pszText) {
  147.         // Source: https://docs.microsoft.com/en-us/windows/win32/controls/create-a-tooltip-for-a-control (modified)
  148.  
  149.         HWND hwndTool, hwndTip;
  150.         TOOLINFO toolInfo;
  151.  
  152.         if (!toolID || !hDlg || !pszText) {
  153.                 return FALSE;
  154.         }
  155.         // Get the window of the tool.
  156.         hwndTool = GetDlgItem(hDlg, toolID);
  157.  
  158.         // Create the tooltip. g_hInst is the global instance handle.
  159.         hwndTip = CreateWindowEx(0, TOOLTIPS_CLASS, NULL,
  160.                 WS_POPUP | TTS_ALWAYSTIP /* | TTS_BALLOON*/,
  161.                 CW_USEDEFAULT, CW_USEDEFAULT,
  162.                 CW_USEDEFAULT, CW_USEDEFAULT,
  163.                 hDlg, NULL,
  164.                 hDllInstance, NULL);
  165.  
  166.         if (!hwndTool || !hwndTip) {
  167.                 return (HWND)NULL;
  168.         }
  169.  
  170.         // Associate the tooltip with the tool.
  171.         memset(&toolInfo, 0, sizeof(TOOLINFO)); // toolInfo = { 0 };
  172.         toolInfo.cbSize = sizeof(toolInfo);
  173.         toolInfo.hwnd = hDlg;
  174.         toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
  175.         toolInfo.uId = (UINT_PTR)hwndTool;
  176.         toolInfo.lpszText = pszText;
  177.         SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
  178.  
  179.         return hwndTip;
  180. }
  181.  
  182. #define IDT_TIMER_INITPREVIEW_DRAW 1111
  183.  
  184. INT_PTR CALLBACK maindlgproc(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam){
  185.         static POINT origpos;
  186.         static Point origscroll;
  187.         static Boolean panning = false;
  188.  
  189.         int item,i;
  190.         POINT newpos;
  191.         DRAWITEMSTRUCT *pdi;
  192.         Point newscroll;
  193.         HGDIOBJ hfnt;
  194.  
  195.         extern Boolean doupdates;
  196.         extern Handle preview_handle;
  197.  
  198.         if ((gdata->pluginDllSliderMessageId != 0) && (wMsg == gdata->pluginDllSliderMessageId)) {
  199.                 // This is for the PLUGIN.DLL sliders only
  200.                 if (doupdates) {
  201.                         int sliderNum = (int)wParam - FIRSTCTLITEM;
  202.                         uint8_t sliderVal = (uint8_t)(lParam & 0xFFFF);
  203.                         slider[sliderNum] = sliderVal;
  204.  
  205.                         SETCTLTEXTINT(hDlg, FIRSTCTLTEXTITEM + sliderNum, sliderVal, false);
  206.                         REPAINTCTL(hDlg, FIRSTCTLTEXTITEM + sliderNum);
  207.  
  208.                         recalc_preview(gpb, hDlg);
  209.                 }
  210.                 return true;
  211.         }
  212.  
  213.         switch (wMsg) {
  214.         case WM_TIMER:
  215.                 switch (wParam)
  216.                 {
  217.                 case IDT_TIMER_INITPREVIEW_DRAW:
  218.                         KillTimer(hDlg, IDT_TIMER_INITPREVIEW_DRAW);
  219.                         recalc_preview(gpb, hDlg);
  220.                         return 0;
  221.                 }
  222.                 break;
  223.         case WM_INITDIALOG:
  224.                 gdata->hWndMainDlg = hDlg;
  225.  
  226.                 if(gdata->standalone){
  227.                         SetWindowTextA(hDlg,gdata->parm.szTitle); // window title bar
  228.                 }
  229.                 centre_window(hDlg);
  230.  
  231.                 hfnt = GetStockObject(ANSI_FIXED_FONT);
  232.  
  233.                 hCurHandOpen = LoadCursorA(hDllInstance, "HAND_OPEN");
  234.                 hCurHandGrab = LoadCursorA(hDllInstance, "HAND_GRAB");
  235.                 hCurHandQuestion = LoadCursorA(hDllInstance, "HAND_QUESTION");
  236.  
  237.                 hIconCautionSign = LoadIconA(hDllInstance, "CAUTION_ICO");
  238.  
  239.                 // Note: The whole class "Preview" gets the mouse cursor, not just the single item!
  240.                 preview_hwnd = GetDlgItem(hDlg, PREVIEWITEM);
  241.                 GetClientRect(preview_hwnd, &preview_rect);
  242.                 SetClassLongPtr(preview_hwnd, GCLP_HCURSOR, (LONG_PTR)hCurHandOpen);
  243.  
  244.                 // Note: The whole class "Caution" gets the mouse cursor, not just the single item!
  245.                 SetClassLongPtr(GetDlgItem(hDlg, FIRSTICONITEM), GCLP_HCURSOR, (LONG_PTR)hCurHandQuestion);
  246.  
  247.                 for(i = 0; i < 4; ++i){
  248.                         CreateToolTip(FIRSTICONITEM + i, hDlg, (TCHAR*)TEXT("Error in expression! Click to see details."));
  249.                 }
  250.  
  251.                 CreateToolTip(ZOOMINITEM, hDlg, (TCHAR*)TEXT("Zoom in"));
  252.                 CreateToolTip(ZOOMOUTITEM, hDlg, (TCHAR*)TEXT("Zoom out"));
  253.                 CreateToolTip(ZOOMLEVELITEM, hDlg, (TCHAR*)TEXT("Fully zoom in/out"));
  254.  
  255.                 for(i = 0; i < 8; ++i){
  256.                         if (gdata->pluginDllSliderMessageId == 0) {
  257.                                 // Non PLUGIN.DLL sliders
  258.                                 SetWindowLongPtr(GetDlgItem(hDlg, FIRSTCTLITEM + i), GWL_STYLE, TBS_HORZ | TBS_AUTOTICKS | WS_CHILD | WS_VISIBLE);
  259.                                 SendDlgItemMessage(hDlg, FIRSTCTLITEM + i, TBM_SETRANGE, TRUE, MAKELONG(0, 255));
  260.                                 SendDlgItemMessage(hDlg, FIRSTCTLITEM + i, TBM_SETTICFREQ, SLIDERPAGE, 0);
  261.                                 SendDlgItemMessage(hDlg, FIRSTCTLITEM + i, TBM_SETPAGESIZE, 0, SLIDERPAGE);
  262.                         }
  263.                         else {
  264.                                 // PLUGIN.DLL sliders
  265.                                 SetSliderRange(GetDlgItem(hDlg, FIRSTCTLITEM + i), 0, 255);
  266.                         }
  267.                         SendDlgItemMessage(hDlg,FIRSTCTLTEXTITEM+i,     EM_SETLIMITTEXT,3,0);
  268.                 }
  269.                 for(i = 0; i < 4; ++i){
  270.                         SendDlgItemMessage(hDlg,FIRSTEXPRITEM+i,        EM_SETLIMITTEXT,MAXEXPR-1,0); // we need 1 byte as NUL terminator, so our formula can be max 1023
  271.                         SendDlgItemMessage(hDlg,FIRSTEXPRITEM+i,        WM_SETFONT,(WPARAM)hfnt,false);
  272.                 }
  273.  
  274.                 maindlginit(hDlg);
  275.  
  276.                 // Some versions of Windows (NT 3.x) won't show the preview if it is calculated here.
  277.                 // So we need to put it in a timer.
  278.                 // Note that 1 millisecond is enough, even if the window needs longer than 1 millisecond to load.
  279.                 //recalc_preview(gpb, hDlg);
  280.                 SetTimer(hDlg, IDT_TIMER_INITPREVIEW_DRAW, 1, (TIMERPROC)NULL);
  281.  
  282.                 break;
  283.         case WM_DESTROY:
  284.                 gdata->hWndMainDlg = 0;
  285.                 DestroyCursor(hCurHandOpen);
  286.                 DestroyCursor(hCurHandGrab);
  287.                 DestroyCursor(hCurHandQuestion);
  288.                 DestroyIcon(hIconCautionSign);
  289.                 break;
  290.         case WM_DRAWITEM:
  291.                 pdi = (DRAWITEMSTRUCT*)lParam;
  292.                 if(pdi->itemAction == ODA_DRAWENTIRE){
  293.                         switch(pdi->CtlID){
  294.                         case PREVIEWITEM:
  295.                                 drawpreview(hDlg,pdi->hDC,PILOCKHANDLE(preview_handle,false));
  296.                                 PIUNLOCKHANDLE(preview_handle);
  297.                                 break;
  298.                         case FIRSTICONITEM:
  299.                         case FIRSTICONITEM + 1:
  300.                         case FIRSTICONITEM + 2:
  301.                         case FIRSTICONITEM + 3:
  302.                                 DrawIcon(pdi->hDC, 0, 0, hIconCautionSign);
  303.                                 break;
  304.                         default:
  305.                                 return false;
  306.                         }
  307.                 }else
  308.                         return false; // we couldn't handle the message
  309.                 break;
  310.         case WM_COMMAND:
  311.                 item = LOWORD(wParam);
  312.                 switch(HIWORD(wParam)){
  313.                 //case BN_CLICKED:
  314.                 case STN_CLICKED:
  315.                         // BN_CLICKED = Button clicked
  316.                         // STN_CLICKED = Static controls with SS_NOTIFY clicked
  317.                         // Both have the same ordinal number
  318.                         if(item==PREVIEWITEM && GetCursorPos(&origpos)){
  319.                                 panning = true;
  320.                                 origscroll = preview_scroll;
  321.                                 SetCursor(hCurHandGrab);
  322.                                 SetCapture(hDlg);
  323.                                 break;
  324.                         }
  325.                 /* ... falls through ... */
  326.                 case EN_CHANGE:
  327.                         if(doupdates && !maindlgitem(hDlg,item))
  328.                                 EndDialog(hDlg,item);
  329.                 }
  330.                 break;
  331. //      case WM_LBUTTONDOWN: break;
  332.         case WM_MOUSEMOVE:
  333.                 if(panning && GetCursorPos(&newpos)){
  334.                         newscroll.h = (int16)(origscroll.h - zoomfactor*((double)newpos.x - (double)origpos.x));
  335.                         newscroll.v = (int16)(origscroll.v - zoomfactor*((double)newpos.y - (double)origpos.y));
  336.                         if( newscroll.h != preview_scroll.h || newscroll.v != preview_scroll.v ){
  337.                                 preview_scroll = newscroll;
  338.                                 recalc_preview(gpb,hDlg);
  339.                         }
  340.                 }
  341.                 break;
  342.         case WM_LBUTTONUP:
  343.                 ReleaseCapture();
  344.                 panning = false;
  345.                 break;
  346.         case WM_HSCROLL:
  347.                 // Only for non-Plugin.dll-sliders
  348.                 item = GetDlgCtrlID((HWND)lParam);
  349.                 if(doupdates && gdata->pluginDllSliderMessageId == 0 && item>=FIRSTCTLITEM && item<=FIRSTCTLITEM+7)
  350.                         slidermoved(hDlg,item);
  351.                 break;
  352.         default:
  353.                 return false;
  354.         }
  355.  
  356.         return true;
  357. }
  358.  
  359. Boolean maindialog(FilterRecordPtr pb){
  360.         PlatformData *p;
  361.         INT_PTR res;
  362.  
  363.         // First try to use the sliders from PLUGIN.DLL (only Photoshop)
  364.         if (!Slider_Init_PluginDll(TEXT("FoundrySlider"))) {
  365.                 // If we couldn't get the sliders from PLUGIN.DLL (probably not running in Photoshop),
  366.                 // then try the Microsoft Trackbar Control instead
  367.                 if (!Slider_Init_MsTrackbar(TEXT("FoundrySlider"))) {
  368.                         // This will happen if we neither have PLUGIN.DLL, nor the Microsoft Trackbar Control (msctls_trackbar32).
  369.                         // "msctls_trackbar32" is not included in Windows NT 3.1, and since there is no OCX or RegSvr32.
  370.                         // It is included in Windows NT 3.5x.
  371.  
  372.                         //simplealert((TCHAR*)TEXT("This plugin requires Photoshop's PLUGIN.DLL or the Microsoft Trackbar Control (msctls_trackbar32) which was not found on your system."));
  373.                         //return false;
  374.  
  375.                         // We simply hide the sliders and let the user enter the numeric values in the edit-box.
  376.                         simplewarning((TCHAR*)TEXT("Visual sliders are not available because neither PLUGIN.DLL, nor the Microsoft Trackbar Control (msctls_trackbar32) was found on your system."));
  377.                         Slider_Init_None(TEXT("FoundrySlider"));
  378.                 }
  379.         }
  380.  
  381.         // For the preview image and caution symbols, we register subclasses, so that we can assign a mouse cursor to this class.
  382.         MakeSimpleSubclass(TEXT("Preview"), TEXT("STATIC"));
  383.         MakeSimpleSubclass(TEXT("Caution"), TEXT("Button"));
  384.  
  385.         // Now show the dialog
  386.         p = (PlatformData*)pb->platformData;
  387.  
  388.         // Note: "Invalid Cursor Handle" is the error when an unrecognized control class is detected
  389.         res = DialogBoxParam(hDllInstance,MAKEINTRESOURCE(gdata->standalone ? ID_PARAMDLG : ID_MAINDLG),
  390.                              (HWND)p->hwnd,maindlgproc,0);
  391.         if (res == 0) {
  392.                 simplealert((TCHAR*)TEXT("DialogBoxParam in valid parent window handle"));
  393.         }
  394.         if (res == -1) {
  395.                 TCHAR s[0x300];
  396.                 xstrcpy(s, (TCHAR*)TEXT("DialogBoxParam failed: "));
  397.                 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, s + xstrlen(s), 0x300 - xstrlen(s), NULL);
  398.                 dbg(&s[0]);
  399.         }
  400.  
  401.         // Clean up after the dialog has been closed
  402.         UnregisterClass(TEXT("Preview"), hDllInstance);
  403.         UnregisterClass(TEXT("Caution"), hDllInstance);
  404.         UnregisterClass(TEXT("FoundrySlider"), hDllInstance);
  405.         Slider_Uninit_PluginDll();
  406.  
  407.         return res == IDOK;
  408. }
  409.