home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / bmviewer.zip / BitmapViewer.c next >
Text File  |  2000-08-26  |  6KB  |  253 lines

  1. #pragma strings(readonly)
  2.  
  3. #define INCL_WINWINDOWMGR
  4. #define INCL_WINFRAMEMGR
  5.  
  6. #ifdef DEBUG_TERM
  7. #define INCL_DOSQUEUES
  8. #define INCL_DOSSESMGR
  9. #define INCL_DOSERRORS
  10. #endif
  11.  
  12.  
  13. #include <os2.h>
  14.  
  15. #include <process.h>
  16.  
  17.  
  18. #ifdef DEBUG_TERM
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <memory.h>
  22. static void launchDebugTerminal(void);
  23. #endif
  24.  
  25. #include "CanvasWindow.h"
  26. #include "WorkerThread.h"
  27.  
  28. #include "resources.h"
  29.  
  30.  
  31.  
  32. /*
  33.  * Function prototypes - external functions
  34.  */
  35. BOOL _Optlink registerCanvasClass(HAB hab);
  36. BOOL _Optlink subclassFrameWindow(HWND hwndFrame);
  37. void _Optlink WorkerThread(void *param);
  38.  
  39.  
  40. /*
  41.  * Function prototypes - local functions
  42.  */
  43. static HWND createFrameWindow(HAB hab);
  44. static HWND createClientWindow(HAB hab, HWND hwndFrame);
  45.  
  46.  
  47. int main(int argc, char *argv[])
  48. {
  49.    int iRet = 0;
  50.    HAB hab = NULLHANDLE;
  51.    HMQ hmq = NULLHANDLE;
  52.    BOOL fSuccess = FALSE;
  53.    HWND hwndFrame = NULLHANDLE;
  54.    HWND hwndClient = NULLHANDLE;
  55.    int tidWorkerThread = -1;
  56.  
  57.    #ifdef DEBUG_TERM
  58.    launchDebugTerminal();
  59.    puts("DebugTerminal ok.");
  60.    #endif
  61.  
  62.  
  63.    /*
  64.     * Initialize PM for thread
  65.     */
  66.    hab = WinInitialize(0UL);
  67.  
  68.    if(hab)
  69.    {
  70.       /*
  71.        * Register custom window class with PM.
  72.        */
  73.       fSuccess = registerCanvasClass(hab);
  74.    }
  75.  
  76.    if(fSuccess)
  77.    {
  78.       /*
  79.        * Create message queue
  80.        */
  81.       hmq = WinCreateMsgQueue(hab, 0L);
  82.    }
  83.  
  84.    if(hmq)
  85.    {
  86.       /*
  87.        * Create application frame window
  88.        */
  89.       hwndFrame = createFrameWindow(hab);
  90.    }
  91.  
  92.    if(hwndFrame)
  93.    {
  94.       /*
  95.        * Create frame client window; this is the main canvas window
  96.        */
  97.       hwndClient = createClientWindow(hab, hwndFrame);
  98.    }
  99.  
  100.    fSuccess = FALSE;
  101.    if(hwndClient)
  102.    {
  103.       /*
  104.        * Subclass the application frame window
  105.        */
  106.       fSuccess = subclassFrameWindow(hwndFrame);
  107.    }
  108.  
  109.    if(fSuccess)
  110.    {
  111.       WRKRPARAMS threadParams = { 0 };
  112.  
  113.       /*
  114.        * Launch Worker Thread. Note: This should only be done once everything else is up and running.
  115.        */
  116.       threadParams.hwndAppFrame = hwndFrame;
  117.       threadParams.hwndCanvas = hwndClient;
  118.       threadParams.argc = argc;
  119.       threadParams.argv = argv;
  120.       tidWorkerThread = _beginthread(WorkerThread, NULL, 32768, &threadParams);
  121.       if(tidWorkerThread != -1)
  122.       {
  123.          WinPostMsg(hwndClient, WMU_CANVAS_NOTIFICATION, MPFROMLONG(WORKERTHREAD_TID), MPFROMLONG(tidWorkerThread));
  124.       }
  125.    }
  126.  
  127.    if(tidWorkerThread != -1)
  128.    {
  129.       QMSG qmsg = { 0 };
  130.  
  131.       /*
  132.        * Enter thread message loop
  133.        */
  134.       while(WinGetMsg(hab, &qmsg, (HWND)NULLHANDLE, 0UL, 0UL))
  135.       {
  136.          WinDispatchMsg(hab, &qmsg);
  137.       }
  138.  
  139.       WinDestroyWindow(hwndFrame);
  140.    }
  141.  
  142.    /*
  143.     * Terminate message queue and PM for thread
  144.     */
  145.    if(hab)
  146.    {
  147.       if(hmq)
  148.       {
  149.          WinDestroyMsgQueue(hmq);
  150.       }
  151.       WinTerminate(hab);
  152.    }
  153.  
  154.    return iRet;
  155. }
  156.  
  157.  
  158.  
  159.  
  160. static HWND createFrameWindow(HAB hab)
  161. {
  162.    HWND hwndFrame = NULLHANDLE;
  163.    LONG lLength = 0;
  164.    char achTitle[256] = "";
  165.  
  166.    /*
  167.     * Load application frame title
  168.     */
  169.    lLength = WinLoadString(hab, (HMODULE)NULLHANDLE, IDS_APPTITLE, sizeof(achTitle), achTitle);
  170.    if(lLength != 0L)
  171.    {
  172.       FRAMECDATA fcd = { sizeof(fcd) };
  173.  
  174.       /*
  175.        * Create frame window
  176.        * See #1 in notes.text
  177.        */
  178.       fcd.flCreateFlags = FCF_SYSMENU | FCF_TASKLIST | FCF_TITLEBAR | FCF_SIZEBORDER | FCF_MINMAX | FCF_MENU | FCF_ACCELTABLE | FCF_ICON | FCF_NOBYTEALIGN;
  179.       fcd.hmodResources = NULLHANDLE;
  180.       fcd.idResources = WIN_APPFRAME;
  181.       hwndFrame = WinCreateWindow(HWND_DESKTOP, WC_FRAME, achTitle, 0UL, 0L, 0L, 0L, 0L, (HWND)NULLHANDLE, HWND_TOP, fcd.idResources, &fcd, NULL);
  182.    }
  183.    return hwndFrame;
  184. }
  185.  
  186.  
  187. static HWND createClientWindow(HAB hab, HWND hwndFrame)
  188. {
  189.    HWND hwndClient = NULLHANDLE;
  190.    LONG lLength = 0;
  191.    char achClass[256] = "";
  192.  
  193.    lLength = WinLoadString(hab, (HMODULE)NULLHANDLE, IDS_CANVASCLASS, sizeof(achClass), achClass);
  194.    if(lLength != 0L)
  195.    {
  196.       hwndClient = WinCreateWindow(hwndFrame, achClass, NULL, WS_VISIBLE, 0L, 0L, 0L, 0L, (HWND)hwndFrame, HWND_TOP, FID_CLIENT, NULL, NULL);
  197.    }
  198.    return hwndClient;
  199. }
  200.  
  201.  
  202.  
  203. #ifdef DEBUG_TERM
  204. static void launchDebugTerminal(void)
  205. {
  206.    APIRET rc = NO_ERROR;
  207.    HFILE pipeDebugRead = 0;
  208.    HFILE pipeDebugWrite = 0;
  209.    STARTDATA sd = { 0 };
  210.    ULONG SessID = 0;
  211.    PID pid = 0;
  212.    HFILE hfNew = 1;                      /* stdout */
  213.    char PgmTitle[30] = "";
  214.    char PgmName[100] = "";
  215.    char szCommandLine[60] = "";
  216.    char ObjBuf[200] = "";
  217.  
  218.    rc = DosCreatePipe(&pipeDebugRead, &pipeDebugWrite, 4096);
  219.    if(rc != NO_ERROR)
  220.    {
  221.       DosBeep(1000, 100);
  222.       exit(42);
  223.    }
  224.  
  225.    _ultoa(pipeDebugRead, szCommandLine, 10);
  226.  
  227.    memset(&sd, 0, sizeof(sd));
  228.  
  229.    sd.Length = sizeof(STARTDATA);
  230.    sd.Related = SSF_RELATED_CHILD;
  231.    sd.FgBg = SSF_FGBG_BACK;
  232.    sd.TraceOpt = SSF_TRACEOPT_NONE;
  233.    memcpy((char*) PgmTitle, "Debug terminal\0", 15);
  234.    sd.PgmTitle = PgmTitle;
  235.    memcpy((char*) PgmName, "PMDebugTerminal.exe\0", 20);
  236.    sd.PgmName = PgmName;
  237.    sd.PgmInputs = szCommandLine;
  238.    sd.InheritOpt = SSF_INHERTOPT_PARENT;
  239.    sd.SessionType = SSF_TYPE_PM;
  240.    sd.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_SETPOS;
  241.    sd.ObjectBuffer = ObjBuf;
  242.    sd.ObjectBuffLen = sizeof(ObjBuf);
  243.  
  244.    rc = DosStartSession(&sd, &SessID, &pid);
  245.    if(rc != NO_ERROR)
  246.    {
  247.       DosBeep(1000, 100);
  248.       exit(43);
  249.    }
  250.    DosDupHandle(pipeDebugWrite, &hfNew);
  251. }
  252. #endif
  253.