home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / os2cl016.zip / provadlg.cpp < prev    next >
C/C++ Source or Header  |  1996-04-25  |  9KB  |  301 lines

  1. /* 
  2.  
  3.  
  4.     provadlg.cpp (emx+gcc) 
  5.  
  6.     1995 Giovanni Iachello
  7.     This is freeware software. You can use or modify it as you wish,
  8.     provided that the part of code that I wrote remains freeware.
  9.     Freeware means that the source code must be available on request 
  10.     to anyone.
  11.     You must also include this notice in all files derived from this
  12.     file.
  13.  
  14.  
  15. */
  16.  
  17. /*
  18.  
  19.     This example shows some of the library features:
  20.     
  21.     * building a PMMainWin and automatic file name management
  22.     * using menus
  23.     * using (smart) dialog boxes
  24.     * calling system dialogs (font dialog)
  25.     * calling message boxes
  26.     * printing from a different thread
  27.     * using the help system and a hook procedure for the message boxes help
  28.  
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include "pmwin.h"
  33. #include "pmdlg.h"
  34. #include "pmgpi.h"
  35. #include "pmhelp.h"
  36. #include "pmstdres.h"
  37. #include "provadlg.h"
  38.  
  39. PMApp* App;
  40.  
  41. PMHelpWin *helpwin;
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // Must process all and only the help requests in message boxes
  45. //
  46.  
  47. BOOL helpHook ( HAB habAnchor, SHORT sMode, USHORT usTopic, USHORT usSubTopic, PRECTL prclPos )
  48. {
  49.     if ( (sMode == HLPM_WINDOW ) && 
  50.          helpwin && 
  51.          ( (usTopic>=PMHLP_DLG_ERRMSG && usTopic<=PMHLP_DLG_ASSERTFAIL) || usTopic==HLP_DLG_INFO) 
  52.        ) {
  53.         helpwin->sendMsg(HM_DISPLAY_HELP, 
  54.             MPFROMLONG (MAKELONG (usTopic, 0)), MPFROMSHORT( HM_RESOURCEID) );
  55.         return TRUE;
  56.     } 
  57.     return FALSE;
  58. }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // Smart dialog box. When an item from the listbox is selected, the string
  62. // is also inserted in the entry field
  63. //
  64.  
  65. class NamesDialog : public PMModalDialog {
  66. public:
  67.     NamesDialog(HWND parent,HWND owner,ULONG id,PMControlMap* cm,void* obj) :
  68.         PMModalDialog(parent,owner,id,cm,obj) {};
  69.     BOOL initdlg() {  // dialog box initialization
  70.         char buf[64];
  71.         PMListBox* lb=(PMListBox*)controlFromID(DNAME_LB_NAMELIST);
  72.         lb->insertItem(LIT_END,"Mario");
  73.         lb->insertItem(LIT_END,"Luca");
  74.         lb->insertItem(LIT_END,"Piero");
  75.         lb->insertItem(LIT_END,"Marco");
  76.         lb->insertItem(LIT_END,"Giovanni");
  77.         ab.loadString( STR_PAOLA, 64, buf);       // load strings from resources
  78.         lb->insertItem( LIT_END, buf);
  79.         ab.loadString( STR_CHIARA, 64, buf);
  80.         lb->insertItem( LIT_END, buf);
  81.         ab.loadString( STR_SILVIA, 64, buf);
  82.         lb->insertItem( LIT_END, buf);
  83.         return TRUE;
  84.     }        
  85.     BOOL control(SHORT id,SHORT ctrlmsg) {
  86.         switch (id) {
  87.         case DNAME_LB_NAMELIST: // control id= the LISTBOX
  88.             switch (ctrlmsg) {
  89.                 case LN_SELECT: {
  90.                     char buf[64];
  91.                     SHORT sIndex;
  92.  
  93.                     PMListBox* ctl=(PMListBox*)controlFromID(DNAME_LB_NAMELIST);
  94.  
  95.                     sIndex=ctl->querySelection();
  96.                     ctl->queryItemText(sIndex,buf,64);
  97.                     
  98.                     controlFromID(DNAME_EF_NAME)->setText(buf); // put string in edit control
  99.                       return TRUE;
  100.                 }
  101.                 case LN_ENTER: // if enter is pressed in the list box, the box quits and accepts.
  102.                     postMsg(WM_COMMAND,MPFROMSHORT(DID_OK),0);
  103.                     return TRUE;
  104.             }
  105.         }
  106.         return FALSE;
  107.     }         
  108. };
  109.  
  110. /////////////////////////////////////////////////////////////////////////////
  111. //    Separate print thread (does not clog the application message queue)
  112. //
  113.  
  114. class PMPrintThread : public PMWindowThread 
  115. {
  116. public:
  117.     PMPrintThread() : PMWindowThread() {
  118.     }
  119.     void main(void* arg=NULL) {
  120.         PMPrinterDC printer(hab);
  121.         printer.open();
  122.         printer.startDoc("provadlg.exe test document");
  123.  
  124.         PMPresSpace *ptr=new PMPresSpace(&printer,0,0,PU_LOMETRIC|PU_LOENGLISH|GPIF_DEFAULT|GPIT_NORMAL|GPIA_ASSOC,hab);
  125.         PMPoint points[4]={ PMPoint(0,0), PMPoint(900,0), PMPoint(0,900), PMPoint(900,900) };
  126.         ptr->move(&points[0]);
  127.         ptr->line(&points[1]);
  128.         ptr->line(&points[2]);
  129.         ptr->line(&points[3]);
  130.         ptr->line(&points[0]);
  131.  
  132.         delete ptr;
  133.         printer.endDoc("provadlg.exe test document");
  134.     }
  135. };
  136.  
  137. /////////////////////////////////////////////////////////////////////////////
  138. // Pippo (this is Disney's Goofy name in Italian) is a PMMainWin with some
  139. // more functions: a client paint function, a menu selection function.
  140.  
  141. class Pippo : public PMMainWin 
  142. {
  143.     struct pluto {
  144.         int lbsel;
  145.         char nome[64];
  146.     } temp;
  147. public:
  148.     Pippo(HAB ab,PMHelpWin* hw) : PMMainWin("pippo.child",ab,hw) {
  149.         createArgs->flCreateFlags|=FCF_MENU|FCF_ICON|FCF_ACCELTABLE;
  150.         createArgs->idResources=ID_PROVADLG;
  151.         createArgs->pszTitle="Pippo";
  152.         strcpy(temp.nome,"Hello");
  153.         temp.lbsel=1;
  154.         caption="Pippo";
  155.         fnFilter="*.*";
  156.         flCaption="Open Pippo File";
  157.         fsCaption="Save Pippo File";
  158.         fileFlags=PMMWFF_UNTITLED;
  159.     } ;
  160.     BOOL paint(PMPresSpace& ps)
  161.     {
  162.         char buf[100];
  163.         sprintf( buf, "ProvaDlg main window '%s'", temp.nome);
  164.         PMRect rcl;
  165.         rcl=this;
  166.           ps.setColor (CLR_DARKCYAN);
  167.           ps.drawText (-1, buf, &rcl, 0, 0, DT_TEXTATTRS | DT_CENTER | DT_VCENTER | DT_ERASERECT);
  168.         return TRUE;
  169.     }
  170.     BOOL command(USHORT id,USHORT cmddev)
  171.     {
  172.         switch (id) {
  173.             case IDM_CRASH: {
  174.                 PMControlMap esempio1cm[] ={
  175.                     cmEntryField(D1_NOME,pluto,nome)
  176.                     cmEnd(D1_NOME)
  177.                 };
  178.                 // *must* be HWND_DESKTOP,hwnd for the help system to work!!!
  179.                 PMModalDialog mydialog(HWND_DESKTOP,hwnd,DLG_ESEMPIO1,esempio1cm,&temp);
  180.                 int ret=mydialog.createWin(); // run dialog
  181.                 if (ret) invalidate(TRUE);
  182.                 return TRUE;
  183.             }
  184.             case IDM_NAMES: {
  185.                 PMControlMap plutocm[] ={
  186.                     cmListBox(DNAME_LB_NAMELIST , pluto,lbsel)
  187.                     cmEntryField(DNAME_EF_NAME,pluto,nome)
  188.                     cmEnd(DNAME_EF_NAME)
  189.                 };
  190.                 NamesDialog names(HWND_DESKTOP,hwnd,DLG_NAME,plutocm,&temp);
  191.  
  192.                 int ret=names.createWin(); // run dialog
  193.                 if (ret) invalidate(TRUE);
  194.                 return TRUE;
  195.             }
  196.             case IDM_FONTS:    {
  197.                 FONTMETRICS fm;
  198.                 PMWindowPresSpace ps(this);
  199.                 ps.queryFontMetrics(&fm);
  200.                 PMFontDialog fd(HWND_DESKTOP,hwnd, &fm,0,"Titolo","Preview");
  201.                 int ret=fd.createWin();
  202.                 assert(ret);
  203.                 FONTDLG fi=fd;
  204. // call this function to test the HELP system in message boxes
  205.                 return WinMessageBox(HWND_DESKTOP,hwnd,"aaa","Result",HLP_DLG_INFO,MB_OK|MB_INFORMATION|MB_HELP);
  206. //                msgBox("Result","%d %s %ld %hd %hd %hd %hd",ret,fi.pszFamilyname,fi.fxPointSize,fi.usWeight,fi.usWidth,fi.x,fi.y);
  207. //                return TRUE;
  208.             }
  209.         }        
  210.         return PMMainWin::command(id,cmddev);
  211.     }
  212. // intercept keyboard (for debug purposes only!)
  213. /*    BOOL kbd(PMEvent& event) {
  214.         char buf[160];
  215.         sprintf(buf,"fs: %u  repeat: %u   scan: %u  chr: %u  vkey: %u",
  216.             event.charmsg->fs,event.charmsg->cRepeat,event.charmsg->scancode,
  217.             event.charmsg->chr,event.charmsg->vkey);
  218.         WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,buf,"KEYBOARDHIT",0,MB_OK);
  219.         return TRUE;
  220.     }*/
  221. // what to do when the user asked to open a file
  222.     BOOL fileOpen(PCSZ filename) {
  223.         msgBox("Result","%s ",filename);
  224.         
  225.         return TRUE;
  226.     }
  227. // what to do when user asked to print the current file
  228.     BOOL filePrint() {
  229.         new PMPrintThread;
  230.         return TRUE;
  231.     }
  232. // react to help messages: tell the keys help, and check for error messages
  233.     BOOL helpmsg(PMEvent &event)
  234.     {
  235.         if (event.msg==HM_QUERY_KEYS_HELP) {
  236.             event.ret=MRFROMSHORT(HLP_KEYS_PROVADLG); 
  237.             return TRUE;
  238.         }
  239.         if (event.msg==HM_HELPSUBITEM_NOT_FOUND) {
  240.             // Don't intercept general help 
  241.             if( SHORT1FROMMP( event.mp1 ) != 1 && SHORT2FROMMP( event.mp2 ) != 2 )
  242.             {
  243.                 ErrBox("IPF Help error.  Topic %lx (subtopic %lx) not found for %s.",
  244.                     (ULONG) SHORT1FROMMP( event.mp2 ),
  245.                     (ULONG) SHORT2FROMMP( event.mp2 ),
  246.                     LONGFROMMP( event.mp1 ) == HLPM_WINDOW ? "application window" :
  247.                         (LONGFROMMP( event.mp1 ) == HLPM_FRAME ? "frame window" : "menu window" ) );
  248.             }
  249.  
  250.             /* For HLPM_WINDOW or HLPM_FRAME return FALSE
  251.                 for no action or TRUE for extended help */
  252.             event.ret=(MRESULT) TRUE;
  253.             return TRUE;
  254.         }
  255.         return FALSE;
  256.     }
  257. };
  258.  
  259.  
  260. int main (int argc,char* argv[])
  261. {
  262.     PMAnchorBlock ab;  // create A.B.
  263.     PMMessageQueue mq; // create M.Q.
  264.     ab.init();         // initialize
  265.     mq.create(ab);
  266.     
  267.     App=new PMApp(ab,mq,argc,argv);
  268.  
  269.     // use help file provadlg.hlp, and connect to ID_PROVADLG help resource definition
  270.     helpwin=new PMHelpWin("Prova Dlg Sample Help File","provadlg.hlp",ID_PROVADLG,ab);
  271.     helpwin->createWin();
  272.  
  273.     // create main window
  274.     Pippo * pip=new Pippo(ab,helpwin);
  275.     pip->createWin();
  276.  
  277.     // set hook for message windows
  278.     ab.setHook(mq,HK_HELP,(PFN)helpHook,NULLHANDLE);
  279.     
  280.     // run the application main loop
  281.     App->run();
  282.  
  283.     // reset hook
  284.     ab.releaseHook(mq,HK_HELP,(PFN)helpHook,NULLHANDLE);
  285.  
  286.     pip->destroyWin();
  287.  
  288.     delete helpwin;
  289.  
  290.     mq.destroy();
  291.     ab.uninit();
  292.  
  293.     return (0);
  294. }
  295.  
  296. /*
  297.  * Local variables:
  298.  * compile-command: "dmake provadlg.exe"
  299.  * end:
  300.  */
  301.