home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1991 / 07_08 / grdlagen / os2kurs / dialog2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-24  |  12.1 KB  |  336 lines

  1. /*--------------------------------------------------------*/
  2. /*                      DIALOG2.C                         */
  3. /*            (C) 1991 J. Heid & toolbox                  */
  4. /*             für OS/2 1.2 oder höher                    */
  5. /*--------------------------------------------------------*/
  6.  
  7. #include <stdio.h>
  8. #include "dialog2.h"         // Symbol. Konstanten für
  9.                              //   dialog2.c und dialog2.rc
  10. #define INCL_WIN
  11. #include <os2.h>             // PM-Include-Datei
  12. #define MAXITEMLEN    80     // max. Länge des Farbtextes
  13. #define ANZAHLFARBEN  16
  14. typedef struct INFO          // Initialisierungsinfo für
  15. {                            //   die Dialogbox:
  16.   USHORT  idActColor;        //   zu checkender Radiobutton,
  17.   USHORT  usType;            //   Typ und
  18.   CHAR   *szTitel;           //   Titel
  19. } INFO;
  20.  
  21. int cdecl        main(int argc, char **argv);
  22. BOOL             WndCreate(VOID);
  23. MRESULT EXPENTRY ClientWindowProc(HWND   hwnd, USHORT msg,
  24.                                   MPARAM mp1,  MPARAM mp2);
  25. MRESULT EXPENTRY FarbenDlgProc   (HWND   hwnd, USHORT msg,
  26.                                   MPARAM mp1,  MPARAM mp2);
  27. BOOL             EnableFarbMenüEintraege(BOOL fEnable);
  28. void             PaintClient(HWND hwnd, USHORT idB);
  29. HAB   hab_g;          // Programmanker
  30. HWND  hwndFrame_g;    // Handle für das Frame-Fenster
  31. HWND  hwndClient_g;   // Handle für den Client-Bereich
  32.                       //        des Fensters
  33. HWND  hwndMenu_g;     // Handle für den Actionbar
  34. CHAR  szText_g[]   =  " Hello World by PM ";
  35. CHAR *aszTitel_g[] =  { "Applikationsmodale Dialogbox",
  36.                         "Systemmodale Dialogbox",
  37.                         "Nichtmodale Dialogbox"  };
  38.                       // Tabelle der Farben
  39. COLOR aColors_g[]  = { CLR_BLACK,     CLR_BLUE,
  40.                        CLR_RED,       CLR_PINK,
  41.                        CLR_GREEN,     CLR_CYAN,
  42.                        CLR_YELLOW,    CLR_WHITE,
  43.                        CLR_DARKGRAY,  CLR_DARKBLUE,
  44.                        CLR_DARKRED,   CLR_DARKPINK,
  45.                        CLR_DARKGREEN, CLR_DARKCYAN,
  46.                        CLR_BROWN,     CLR_PALEGRAY };
  47.         // Text neben den Radiobuttons in der Dialogbox
  48. CHAR  aszRadioButtonText_g[ANZAHLFARBEN][MAXITEMLEN+1];
  49. BOOL  fMehrereDlgBoxen_g = FALSE;
  50.  
  51. int cdecl main(int argc, char **argv)
  52. {
  53.   HMQ   hmq;          // Handle für die Message-Queue
  54.   QMSG  qmsg;         // Message in der Message-Queue
  55.   if (argc == 2)
  56.   {
  57.     if ((argv[1][0] == '-' || argv[1][0] == '/') &&
  58.           (argv[1][1] == 'm'))
  59.       fMehrereDlgBoxen_g = TRUE;
  60.   }
  61.   hab_g = WinInitialize(0);  // Initialisiere PM
  62.                              // Erzeuge Message-Queue
  63.   hmq = WinCreateMsgQueue(hab_g, 0);
  64.   if (hmq != (HMQ)NULL)
  65.   {
  66.     if (WndCreate() == TRUE)
  67.     {
  68.       while(WinGetMsg(hab_g, (PQMSG)&qmsg, (HWND)NULL, 0,0))
  69.         WinDispatchMsg(hab_g, (PQMSG)&qmsg);
  70.       WinDestroyWindow(hwndFrame_g);
  71.     }
  72.     WinDestroyMsgQueue(hmq);
  73.   }
  74.   WinTerminate(hab_g);
  75.   return 0;
  76. }
  77.  
  78. BOOL WndCreate(VOID)
  79. {
  80.                         // Name der Fensterklasse
  81.   CHAR  szClientClass[] = "KlasseDialog2";
  82.   ULONG flCreateFrame;  // Flaggen für die Erzeugung
  83.                         //         der Controls
  84.   BOOL  fRc;            // Hilfsvariable zum Abprüfen
  85.                         //    des Rückgabewertes von
  86.                         //    API-Funktionen
  87.   // Ordne die ClientWndProc einer Klasse zu
  88.   fRc = WinRegisterClass(
  89.           hab_g,            // Programmanker
  90.           szClientClass,    // Name der Fensterklasse
  91.           ClientWindowProc, // Adr. der Fensterprozedur
  92.           CS_SIZEREDRAW,    // Klassen-Style
  93.           0);               // keine Extra-Bytes reservieren
  94.   // wenn WinRegisterClass schiefgeht
  95.   if (fRc == FALSE) return (FALSE);
  96.            // Standardfenster mit Actionbar und
  97.            //     Akzeleratortabelle als Resource
  98.   flCreateFrame = FCF_STANDARD & ~FCF_ICON;
  99.            // Erzeugen eines Standardfensters
  100.   hwndFrame_g = WinCreateStdWindow(
  101.          HWND_DESKTOP,  // Handle des Vaterfensters
  102.          WS_VISIBLE,    // Style des Frame-Fensters
  103.          (PULONG)&flCreateFrame,
  104.          szClientClass, // Client-Fenster-Klasse
  105.                         // expliziten Titel anhängen
  106.          " - Demonstration der Dialogbox-Modalitäten",
  107.          0L,            // Style des Client-Fensters
  108.          (HMODULE)NULL, // Resourcen sind in EXE-Datei
  109.          ID_RESOURCE,   // Bezeichner für Resourcen
  110.          (PHWND)&hwndClient_g); // Zeiger auf den Handle
  111.                                 //   des Client-Fensters
  112.                // WinCreateStdWindow nicht erfolgreich
  113.   if (hwndFrame_g == (HWND)NULL) return (FALSE);
  114.   return (TRUE);
  115. }
  116.  
  117. MRESULT EXPENTRY ClientWindowProc(HWND hwnd, USHORT msg,
  118.                                   MPARAM mp1, MPARAM mp2)
  119. {
  120.   static  BOOL    fDlgBoxActive_s   = FALSE;
  121.   static  USHORT  idColorBClient_s  =  ID_BRED;
  122.   static  USHORT  idColorBClientOld_s;
  123.   USHORT  usRc;
  124.   BOOL    fRc;
  125.   switch(msg)
  126.   {
  127.     case WM_CREATE:
  128.     {
  129.       HWND    hwndDlg;
  130.       INFO    info;
  131.       USHORT  usItemLen;
  132.       int     i;
  133.       info.idActColor = idColorBClient_s;
  134.       hwndDlg = WinLoadDlg(HWND_DESKTOP, hwnd, 
  135.                            FarbenDlgProc, (HMODULE) NULL,
  136.                            IDD_BCOLOR, (PVOID)&info);
  137.       if (hwndDlg == NULL) return ((MRESULT)TRUE);
  138.       for (i = 0; i <= ID_BPGRAY-ID_BBLACK; i++)
  139.       {
  140.         usItemLen = WinQueryDlgItemText(
  141.                       hwndDlg, ID_BBLACK+i, MAXITEMLEN,
  142.                       aszRadioButtonText_g[i]);
  143.       }
  144.       fRc = WinDestroyWindow(hwndDlg);
  145.       if (fRc != TRUE) return ((MRESULT)TRUE);
  146.       hwndFrame_g = WinQueryWindow(hwnd, QW_PARENT, FALSE);
  147.       hwndMenu_g = WinWindowFromID(hwndFrame_g, FID_MENU);
  148.       if (hwndFrame_g == NULL || hwndMenu_g == NULL)
  149.         return ((MRESULT)TRUE);
  150.       return ((MRESULT)FALSE);
  151.     }
  152.     case WM_PAINT:
  153.       PaintClient (hwnd, idColorBClient_s);
  154.       return 0;
  155.     case WM_USER:
  156.     {
  157.       USHORT idRadioButton;
  158.       idRadioButton = SHORT1FROMMP(mp1);
  159.       if (idRadioButton != idColorBClient_s &&
  160.             idRadioButton >= ID_BBLACK      &&
  161.             idRadioButton <= ID_BPGRAY)
  162.       {
  163.         idColorBClient_s = idRadioButton;
  164.         DosBeep(1200, 100);
  165.         fRc = WinInvalidateRect (hwnd, NULL, FALSE) ;
  166.       }
  167.       return 0 ;                // Message wurde verarbeitet
  168.     }
  169.     case WM_COMMAND:
  170.     {
  171.       USHORT  command;
  172.       INFO    info;
  173.       command = COMMANDMSG(&msg)->cmd;
  174.       switch (command)
  175.       {
  176.         case IDM_EXITPROG:
  177.           WinPostMsg(hwnd, WM_CLOSE, 0L, 0L);
  178.           break;
  179.         case IDM_APPMODAL:
  180.         case IDM_SYSMODAL:
  181.         case IDM_NONMODAL:
  182.         {
  183.           HWND    hwndDlgBoxOwner;
  184.           idColorBClientOld_s = idColorBClient_s;
  185.           info.idActColor     = idColorBClient_s;
  186.           if (command == IDM_APPMODAL)
  187.           {
  188.             info.usType     = IDM_APPMODAL;
  189.             info.szTitel    = aszTitel_g[0];
  190.             hwndDlgBoxOwner = hwnd;
  191.           }
  192.           else if (command == IDM_SYSMODAL)
  193.           {
  194.             info.usType     = IDM_SYSMODAL;
  195.             info.szTitel    = aszTitel_g[1];
  196.             hwndDlgBoxOwner = hwnd;
  197.           }
  198.           else if (command == IDM_NONMODAL)
  199.           {
  200.             info.usType     = IDM_NONMODAL;
  201.             info.szTitel    = aszTitel_g[2];
  202.             hwndDlgBoxOwner = HWND_DESKTOP;
  203.           }
  204.           // Aufbau einer Dialogbox
  205.           if (fMehrereDlgBoxen_g == TRUE  ||
  206.              (fMehrereDlgBoxen_g == FALSE &&
  207.               fDlgBoxActive_s == FALSE))
  208.           {
  209.             fDlgBoxActive_s = TRUE;
  210.             // Farb-Menüeinträge disablen
  211.             if (command == IDM_NONMODAL &&
  212.                   fMehrereDlgBoxen_g == FALSE)
  213.             { fRc = EnableFarbMenueEinträge(FALSE); }
  214.             usRc = WinDlgBox (HWND_DESKTOP, hwndDlgBoxOwner,
  215.                               FarbenDlgProc, (HMODULE) NULL,
  216.                               IDD_BCOLOR, (PVOID)&info);
  217.             // Farb-Menüeinträge wieder enablen
  218.             if (command == IDM_NONMODAL &&
  219.                 fMehrereDlgBoxen_g == FALSE)
  220.             { fRc = EnableFarbMenueEintraege(TRUE); }
  221.             fDlgBoxActive_s = FALSE;
  222.             if (usRc != DID_ERROR && usRc == DID_CANCEL &&
  223.                idColorBClient_s != idColorBClientOld_s)
  224.             {
  225.               idColorBClient_s  =  idColorBClientOld_s;
  226.               DosBeep(1200, 100);
  227.               fRc = WinInvalidateRect(hwnd,NULL,FALSE);
  228.             }
  229.           }   // Ende des Aufbau einer Dialogbox
  230.           return 0 ;
  231.         }  // Ende case
  232.         case IDM_RESUME:
  233.           break;
  234.         default:            // default für switch(command)
  235.           return WinDefWindowProc(hwnd,msg,mp1,mp2);
  236.       }
  237.     }
  238.     default:                // default für switch(msg)
  239.       return WinDefWindowProc(hwnd, msg, mp1, mp2);
  240.   }
  241.   return FALSE;
  242. }
  243.  
  244. void PaintClient(HWND hwnd, USHORT idB)
  245. {
  246.   HPS    hps;               // Presentation-Space-Handle
  247.   RECTL  rectl;             // Struktur Rechteck-Koordinaten
  248.   BOOL   fRc;
  249.   int    rc;
  250.   char   szTmp[255];
  251.   COLOR  colorBClient;   // Erzeuge einen Presentation Space
  252.   hps = WinBeginPaint(hwnd, NULL, NULL);
  253.   sprintf(szTmp, "%s (Farbe = %s)", szText_g,
  254.           aszRadioButtonText_g[idB-ID_BBLACK]);
  255.   colorBClient = aColors_g[idB-ID_BBLACK];
  256.         // rect erhält die Koordinaten des Client
  257.         // fRc = TRUE, wenn WinQueryWindowRect ok ist
  258.   fRc = WinQueryWindowRect(hwnd, &rectl);
  259.         // rc = Anzahl geschriebener Zeichen
  260.   rc = WinDrawText(hps, -1, szTmp, &rectl, CLR_WHITE, 
  261.                    colorBClient, 
  262.                    DT_CENTER | DT_VCENTER | DT_ERASERECT);
  263.   fRc = WinEndPaint(hps);   // Paint ist erledigt
  264. }
  265.  
  266. MRESULT EXPENTRY FarbenDlgProc(HWND hwnd,  USHORT msg,
  267.                                MPARAM mp1, MPARAM mp2)
  268. {
  269.   static USHORT idRadioButton_s;
  270.   BOOL          fRc;
  271.   switch (msg)
  272.   {
  273.     case WM_INITDLG:
  274.     {
  275.       INFO FAR *pinfo;
  276.       pinfo = (INFO FAR *) PVOIDFROMMP(mp2);
  277.       idRadioButton_s = pinfo->idActColor;
  278.       WinSendDlgItemMsg(hwnd, idRadioButton_s, BM_SETCHECK, 
  279.                         MPFROM2SHORT (TRUE, 0), NULL);
  280.       // Titel der Dialogbox
  281.       fRc = WinSetWindowText(hwnd, pinfo->szTitel);
  282.       // Dialogbox systemmodal machen
  283.       if (pinfo->usType == IDM_SYSMODAL)
  284.         fRc = WinSetSysModalWindow(HWND_DESKTOP, hwnd);
  285.       return (MRESULT)FALSE ;
  286.     }
  287.     case WM_CONTROL:
  288.     {
  289.       USHORT  id = SHORT1FROMMP (mp1);
  290.       if (id >= ID_BBLACK  &&  id <= ID_BPGRAY)
  291.       {
  292.         if (SHORT2FROMMP(mp1) == BN_CLICKED)
  293.         {
  294.           fRc = WinPostMsg(hwndClient_g, WM_USER,
  295.                           MPFROMSHORT(idRadioButton_s), 0L);
  296.           if (fRc == TRUE) idRadioButton_s = id;
  297.         }
  298.       }
  299.       return 0 ;
  300.     }
  301.     default:   // default für switch(msg)
  302.       return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  303.   }
  304. }
  305.  
  306. BOOL EnableFarbMenueEintraege(BOOL fEnable)
  307. {
  308.   BOOL fRc1, fRc2, fRc3;
  309.   fRc1 = (BOOL) SHORT1FROMMR(
  310.   WinSendMsg(hwndMenu_g,
  311.              MM_SETITEMATTR,
  312.              MPFROM2SHORT(IDM_APPMODAL, TRUE),
  313.              MPFROM2SHORT(MIA_DISABLED,
  314.                           fEnable ? ~MIA_DISABLED:
  315.                                      MIA_DISABLED)));
  316.   fRc2 = (BOOL) SHORT1FROMMR(
  317.   WinSendMsg(hwndMenu_g,
  318.              MM_SETITEMATTR,
  319.              MPFROM2SHORT(IDM_SYSMODAL, TRUE),
  320.              MPFROM2SHORT(MIA_DISABLED,
  321.                           fEnable ? ~MIA_DISABLED:
  322.                                      MIA_DISABLED)));
  323.   fRc3 = (BOOL) SHORT1FROMMR(
  324.   WinSendMsg(hwndMenu_g,
  325.              MM_SETITEMATTR,
  326.              MPFROM2SHORT(IDM_NONMODAL, TRUE),
  327.              MPFROM2SHORT(MIA_DISABLED,
  328.                           fEnable ? ~MIA_DISABLED:
  329.                                      MIA_DISABLED)));
  330.   if (!fRc1 || !fRc2 || !fRc3) return FALSE;
  331.   else return TRUE;
  332.  }
  333.  
  334. /*--------------------------------------------------------*/
  335. /*                  Ende von DIALOG2.C                    */
  336.