home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / tplab009.zip / tapelab.cpp < prev    next >
C/C++ Source or Header  |  1996-05-05  |  15KB  |  474 lines

  1. /* 
  2.  
  3.  
  4.     tapelab.cpp (emx+gcc) 
  5.     Tape Label Editor Version 0.09
  6.  
  7.     1995 1996 Giovanni Iachello
  8.     This is freeware software. You can use or modify it as you wish,
  9.     provided that the part of code that I wrote remains freeware.
  10.     Freeware means that the source code must be available on request 
  11.     to anyone.
  12.     You must also include this notice in all files derived from this
  13.     file.
  14.  
  15.  
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include "tapelab.h"
  23. #include "pmstdres.h"
  24. #include "pmhelp.h"
  25.  
  26.  
  27. ////////////////////////////////////////////////////////////////////////////
  28.  
  29.  
  30. TapeLab::TapeLab(HAB ab,char* file) : PMMainWin("tapelab",ab,helpwin) 
  31. {        
  32.     createArgs->flCreateFlags|=FCF_MENU|FCF_ICON|FCF_ACCELTABLE|FCF_VERTSCROLL;
  33.     createArgs->idResources=ID_TAPELAB;
  34.     createArgs->pszTitle="Tape Lab - (untitled)";
  35.     caption="Tape Lab";
  36.     fnFilter="*.TL";
  37.     flCaption="Open Tape Label File";
  38.     fsCaption="Save Tape Label File";
  39.     fileFlags=PMMWFF_UNTITLED;
  40.     tempfile=file;
  41.     sb=NULL;
  42. //    sizey = vertical size in pixels of the client window
  43. //    posy = vertical position in pixels of the client window viewport
  44. //         in the whole vitual client window
  45.     posy=-1;                  // must be set for the first time
  46.     sizey=-1;
  47. }
  48.  
  49. TapeLab::~TapeLab()
  50. {
  51.     delete sb;    
  52. }
  53.  
  54. BOOL TapeLab::createWin() 
  55. {
  56.     PMMainWin::createWin();
  57.     sb=new PMVertScrollBar(this,NULL);
  58.     if (tempfile)
  59.         if (fileOpen(tempfile)) {
  60.             strcpy(filename,tempfile);
  61.             fileFlags=PMMWFF_FILE;
  62.             setTitleCaption();
  63.             tempfile=NULL;
  64.         }
  65.     return TRUE;
  66. }    
  67.  
  68. BOOL TapeLab::size(SHORT cx,SHORT cy)
  69. {
  70.     if (posy!=-1) {         // pos is better set the first time by paint().
  71.         posy=posy+sizey-cy;
  72.     }
  73.     sizey=cy;       // update window dimentions          
  74.     return TRUE;
  75. }
  76.  
  77. BOOL TapeLab::other(PMEvent &event)
  78. {
  79.     if (event.msg==WM_VSCROLL) {      // gestione scroll bar
  80.         PMWindowPresSpace ps(this);      // crea pres space della finestra
  81.         SIZEL size;
  82.         size.cx=1500;
  83.         size.cy=fixpoints[labelformat.size][0].y+100;
  84.         ps.set(&size,PU_LOMETRIC);
  85.         PMRect q;
  86.         ps.queryPageViewport(&q);
  87.         switch (SHORT2FROMMP(event.mp2) ) {
  88.             case SB_SLIDERPOSITION: {
  89.                 posy=q.yTop-sizey-SHORT1FROMMP(event.mp2);
  90.                 invalidate(TRUE);        
  91.                 return TRUE;
  92.             }
  93.             case SB_LINEUP: {
  94.                 posy=min(q.yTop-sizey,posy+10);
  95.                 invalidate(TRUE);        
  96.                 return TRUE;
  97.             }
  98.             case SB_LINEDOWN: {
  99.                 posy=max(0,posy-10);
  100.                 invalidate(TRUE);        
  101.                 return TRUE;
  102.             }
  103.             case SB_PAGEUP: {
  104.                 SWP swp;
  105.                 queryPos(&swp);
  106.                 posy=min(q.yTop-sizey,posy+swp.cy);
  107.                 invalidate(TRUE);        
  108.                 return TRUE;
  109.             }
  110.             case SB_PAGEDOWN: {
  111.                 SWP swp;
  112.                 queryPos(&swp);
  113.                 posy=max(0,posy-swp.cy);
  114.                 invalidate(TRUE);        
  115.                 return TRUE;
  116.             }
  117.         }
  118.     }
  119.     return FALSE;
  120. }    
  121.  
  122. BOOL TapeLab::command(USHORT id,USHORT cmddev)
  123. {
  124.     switch (id) {
  125. /////////////////////////////////////////////////////////////////////////////
  126. // Menu selection IDM_TITLE : user wants to change label title
  127. //
  128.            case IDM_TITLE:    {
  129.             struct _temp {
  130.                 char t1[200];
  131.                 char t2[200];
  132.             };
  133.             _temp *temp=new _temp;
  134.             strcpy(temp->t1,labelformat.title1);
  135.             strcpy(temp->t2,labelformat.title2);
  136.             static PMControlMap titlectrlmap[]={
  137.                 cmEntryField(DTITLE_EF_T1,_temp,t1)
  138.                 cmEntryField(DTITLE_EF_T2,_temp,t2)
  139.                 cmEnd(DTITLE_EF_T1)
  140.             };
  141.             PMModalDialog titledlg(HWND_DESKTOP,hwnd,DLG_TITLE,titlectrlmap,temp);
  142.             int ret=titledlg.createWin();
  143.             if (ret) {
  144.                 delete labelformat.title1;
  145.                 delete labelformat.title2;
  146.                 labelformat.title1=new char[strlen(temp->t1)+1];
  147.                 labelformat.title2=new char[strlen(temp->t2)+1];
  148.                 strcpy(labelformat.title1,temp->t1);
  149.                 strcpy(labelformat.title2,temp->t2);
  150.                 setModified(TRUE);
  151.                 invalidate(TRUE);
  152.             }
  153.             delete temp;
  154.             return TRUE;
  155.         }
  156. /////////////////////////////////////////////////////////////////////////////
  157. // Menu selection IDM_TEXT : user wants to change label text
  158. //
  159.            case IDM_TEXT:    {
  160.             struct _temp1 {
  161.                 char t1[3000];
  162.                 char t2[3000];
  163.             };
  164.             _temp1 *temp=new _temp1;
  165.             strcpy(temp->t1,labelformat.text1);
  166.             strcpy(temp->t2,labelformat.text2);
  167.  
  168.             static PMControlMap textctrlmap[]={
  169.                 cmMultiLineEdit(DTEXT_MLE_T1,_temp1,t1)
  170.                 cmMultiLineEdit(DTEXT_MLE_T2,_temp1,t2)
  171.                 cmEnd(DTEXT_MLE_T1)
  172.             };
  173.             PMModalDialog textdlg(HWND_DESKTOP,hwnd,DLG_TEXT,textctrlmap,temp);
  174.             int ret=textdlg.createWin();
  175.             if (ret) {
  176.                 delete labelformat.text1;
  177.                 delete labelformat.text2;
  178.                 labelformat.text1=new char[strlen(temp->t1)+1];
  179.                 labelformat.text2=new char[strlen(temp->t2)+1];
  180.                 strcpy(labelformat.text1,temp->t1);
  181.                 strcpy(labelformat.text2,temp->t2);
  182.                 setModified(TRUE);
  183.                 invalidate(TRUE);
  184.             }
  185.             delete temp;
  186.             return TRUE;
  187.         }
  188. /////////////////////////////////////////////////////////////////////////////
  189. // Menu selection IDM_SIZE : user wants to change label size
  190. //
  191.            case IDM_SIZE:    {
  192.             struct _temp2 {
  193.                 int size;
  194.                 int flaps;
  195.                 char width[32];
  196.                 char flap1[32];
  197.                 char flap2[32];
  198.                 char flap3[32];
  199.                 char flap4[32];
  200.             } temp;
  201.             temp.size=labelformat.size;
  202.             temp.flaps=labelformat.flaps;
  203.             sprintf(temp.width,"%d",labelformat.customsize.width);
  204.             sprintf(temp.flap1,"%d",labelformat.customsize.flap[0]);
  205.             sprintf(temp.flap2,"%d",labelformat.customsize.flap[1]);
  206.             sprintf(temp.flap3,"%d",labelformat.customsize.flap[2]);
  207.             sprintf(temp.flap4,"%d",labelformat.customsize.flap[3]);
  208.             static PMControlMap sizectrlmap[]={
  209.                 cmRadioButtonGroup(DSIZE_RB_NORMAL,_temp2,size)
  210.                 cmSpinButton(DSIZE_SB_FLAPS,_temp2,flaps)
  211.                 cmEntryField(DSIZE_EF_WIDTH,_temp2,width)
  212.                 cmEntryField(DSIZE_EF_FLAP1,_temp2,flap1)
  213.                 cmEntryField(DSIZE_EF_FLAP2,_temp2,flap2)
  214.                 cmEntryField(DSIZE_EF_FLAP3,_temp2,flap3)
  215.                 cmEntryField(DSIZE_EF_FLAP4,_temp2,flap4)
  216.                 cmEnd(DSIZE_RB_NORMAL)
  217.             };
  218.             SizeDlg sizedlg(hwnd, sizectrlmap, &temp);
  219.             int ret=sizedlg.createWin();
  220.             if (ret) {
  221.                 labelformat.size=temp.size;
  222.                 labelformat.flaps=temp.flaps;
  223.                 labelformat.customsize.width=strtol(temp.width,NULL,0);
  224.                 labelformat.customsize.flap[0]=strtol(temp.flap1,NULL,0);
  225.                 labelformat.customsize.flap[1]=strtol(temp.flap2,NULL,0);
  226.                 labelformat.customsize.flap[2]=strtol(temp.flap3,NULL,0);
  227.                 labelformat.customsize.flap[3]=strtol(temp.flap4,NULL,0);
  228.                 labelformat.writecustomfixpoints(); // write new custom points in global data structure..
  229.                 setModified(TRUE);
  230.                 posy=-1;                // recalculate the scroll bar
  231.                 sizey=-1;
  232.                 invalidate(TRUE);
  233.             }
  234.             return TRUE;
  235.         }
  236. /////////////////////////////////////////////////////////////////////////////
  237. // Menu selection IDM_INFO : user wants to change label information
  238. //
  239.            case IDM_INFO:    {
  240.             struct _temp3 {
  241.                 char date1[40];
  242.                 int nr1;
  243.                 int source1;
  244.                 char date2[40];
  245.                 int nr2;
  246.                 int source2;
  247.                 int whatinfo[6];
  248.             } temp;
  249.             strcpy(temp.date1,labelformat.date1);
  250.             strcpy(temp.date2,labelformat.date2);
  251.             temp.nr1=labelformat.nr1;
  252.             temp.nr2=labelformat.nr2;
  253.             temp.source1=labelformat.source1;
  254.             temp.source2=labelformat.source2;
  255.             for (int i=0; i<6; i++) temp.whatinfo[i]=labelformat.whatinfo[i];
  256.  
  257.             static PMControlMap infoctrlmap[]={
  258.                 cmEntryField(DINFO_EF_DATE1,_temp3,date1)
  259.                 cmSpinButton(DINFO_SB_SOURCE1,_temp3,source1)
  260.                 cmSpinButton(DINFO_SB_NR1,_temp3,nr1)
  261.                 cmEntryField(DINFO_EF_DATE2,_temp3,date2)
  262.                 cmSpinButton(DINFO_SB_SOURCE2,_temp3,source2)
  263.                 cmSpinButton(DINFO_SB_NR2,_temp3,nr2)
  264.                 cmCheckBox(DINFO_CB_BASE0,_temp3,whatinfo[0])
  265.                 cmCheckBox(DINFO_CB_BASE1,_temp3,whatinfo[1])
  266.                 cmCheckBox(DINFO_CB_BASE2,_temp3,whatinfo[2])
  267.                 cmCheckBox(DINFO_CB_BASE3,_temp3,whatinfo[3])
  268.                 cmCheckBox(DINFO_CB_BASE4,_temp3,whatinfo[4])
  269.                 cmCheckBox(DINFO_CB_BASE5,_temp3,whatinfo[5])
  270.                 cmEnd(DINFO_EF_DATE1)
  271.             };
  272.             InfoDlg infodlg(hwnd, infoctrlmap, &temp);
  273.             int ret=infodlg.createWin();
  274.             if (ret) {
  275.                 delete labelformat.date1;
  276.                 delete labelformat.date2;
  277.                 labelformat.date1=new char[strlen(temp.date1)+1];
  278.                 labelformat.date2=new char[strlen(temp.date2)+1];
  279.                 strcpy(labelformat.date1,temp.date1);
  280.                 strcpy(labelformat.date2,temp.date2);
  281.                 labelformat.nr1=temp.nr1;
  282.                 labelformat.nr2=temp.nr2;
  283.                 labelformat.source1=temp.source1;
  284.                 labelformat.source2=temp.source2;
  285.                 for (int i=0; i<6; i++) labelformat.whatinfo[i]=temp.whatinfo[i];
  286.                 setModified(TRUE);
  287.                 invalidate(TRUE);
  288.             }
  289.             return TRUE;
  290.         }
  291. /////////////////////////////////////////////////////////////////////////////
  292. // Menu selection IDM_FORM : user wants to change label format
  293. //
  294.            case IDM_FORM:    {
  295.             struct _temp4 {
  296.                 char titlelm[32]; // margine sin. titolo
  297.                 char textlm[32],texttm[32]; // margine sinistro e superiore testo
  298.                 char tabsize[32]; // dimensione tabs
  299.                 char textvert1[32],textvert2[32];    // permillesimi di spaziatura orizzontale e verticale
  300.                 char texthoriz1[32],texthoriz2[32];
  301.             };
  302.             _temp4 *temp=new _temp4;
  303.             sprintf(temp->tabsize,"%d",labelformat.form.tabsize);
  304.             sprintf(temp->titlelm,"%d",labelformat.form.titlelm);
  305.             sprintf(temp->textlm,"%d",labelformat.form.textlm);
  306.             sprintf(temp->texttm,"%d",labelformat.form.texttm);
  307.             sprintf(temp->textvert1,"%d",labelformat.form.textvert1);
  308.             sprintf(temp->texthoriz1,"%d",labelformat.form.texthoriz1);
  309.             sprintf(temp->textvert2,"%d",labelformat.form.textvert2);
  310.             sprintf(temp->texthoriz2,"%d",labelformat.form.texthoriz2);
  311.             static PMControlMap formctrlmap[]={
  312.                 cmEntryField(DFORM_EF_TITLM,_temp4,titlelm)
  313.                 cmEntryField(DFORM_EF_TABSIZE,_temp4,tabsize)
  314.                 cmEntryField(DFORM_EF_TLM,_temp4,textlm)
  315.                 cmEntryField(DFORM_EF_TTM,_temp4,texttm)
  316.                 cmEntryField(DFORM_EF_THSA,_temp4,texthoriz1)
  317.                 cmEntryField(DFORM_EF_THSB,_temp4,texthoriz2)
  318.                 cmEntryField(DFORM_EF_TVSA,_temp4,textvert1)
  319.                 cmEntryField(DFORM_EF_TVSB,_temp4,textvert2)
  320.                 cmEnd(DFORM_EF_TITLM)
  321.             };
  322.             PMModalDialog titledlg(HWND_DESKTOP,hwnd,DLG_FORMAT,formctrlmap,temp);
  323.             int ret=titledlg.createWin();
  324.             if (ret) {
  325.                 labelformat.form.tabsize=strtol(temp->tabsize,NULL,0);
  326.                 labelformat.form.titlelm=strtol(temp->titlelm,NULL,0);
  327.                 labelformat.form.textlm=strtol(temp->textlm,NULL,0);
  328.                 labelformat.form.texttm=strtol(temp->texttm,NULL,0);
  329.                 labelformat.form.textvert1=strtol(temp->textvert1,NULL,0);
  330.                 labelformat.form.textvert2=strtol(temp->textvert2,NULL,0);
  331.                 labelformat.form.texthoriz1=strtol(temp->texthoriz1,NULL,0);
  332.                 labelformat.form.texthoriz2=strtol(temp->texthoriz2,NULL,0);
  333.                 setModified(TRUE);
  334.                 invalidate(TRUE);
  335.             }
  336.             delete temp;
  337.             return TRUE;
  338.         }
  339. /////////////////////////////////////////////////////////////////////////////
  340. // Menu selection IDM_SAVE : user wants to save settings
  341. //
  342.            case IDM_SAVE:    {
  343.                PMUserProfile myprofile;
  344.                myprofile.writeData(SA_APPLNAME,SA_SETTINGSKEY,&labelformat,sizeof(labelformat));
  345.             return TRUE;
  346.         }
  347. /////////////////////////////////////////////////////////////////////////////
  348. // Menu selection IDM_FON_* : user wants to change various fonts
  349. //
  350.            case IDM_FON_TIA:    
  351.             setFont(&labelformat.title1font,"Title Side A Font"); 
  352.             return TRUE;
  353.            case IDM_FON_TIB:    
  354.             setFont(&labelformat.title2font,"Title Side B Font"); 
  355.             return TRUE;
  356.            case IDM_FON_TXA:    
  357.             setFont(&labelformat.text1font,"Text Side A Font");
  358.             return TRUE;
  359.            case IDM_FON_TXB:    
  360.             setFont(&labelformat.text2font,"Text Side B Font");
  361.             return TRUE;
  362.            case IDM_FON_DATE:    
  363.             setFont(&labelformat.datefont,"Date Font"); 
  364.             return TRUE;
  365.            case IDM_FON_SOURCE: 
  366.             setFont(&labelformat.sourcefont,"Source Font");
  367.             return TRUE;
  368.            case IDM_FON_NR: 
  369.             setFont(&labelformat.nrfont,"NR Font");
  370.             return TRUE;
  371.     }
  372.     return PMMainWin::command(id,cmddev);
  373. }
  374.  
  375. BOOL TapeLab::helpmsg(PMEvent &event)
  376. {
  377.     if (event.msg==HM_QUERY_KEYS_HELP) {
  378.         event.ret=MRFROMSHORT(HLP_KEYS_TAPELAB); 
  379.         return TRUE;
  380.     }
  381.     if (event.msg==HM_HELPSUBITEM_NOT_FOUND) {
  382.         // Don't intercept general help 
  383.         if( SHORT1FROMMP( event.mp1 ) != 1 && SHORT2FROMMP( event.mp2 ) != 2 ) 
  384.             ErrBox("IPF Help error.  Topic %lx (subtopic %lx) not found for %s.",
  385.                    (ULONG) SHORT1FROMMP( event.mp2 ),
  386.                    (ULONG) SHORT2FROMMP( event.mp2 ),
  387.                    LONGFROMMP( event.mp1 ) == (LONG)HLPM_WINDOW ? "application window" :
  388.                    (LONGFROMMP( event.mp1 ) == (LONG)HLPM_FRAME ? "frame window" : "menu window" ) );
  389.  
  390.         // For HLPM_WINDOW or HLPM_FRAME return FALSE
  391.         // for no action or TRUE for extended help 
  392.         event.ret=(MRESULT) TRUE;
  393.         return TRUE;
  394.     }
  395.     return FALSE;
  396. }
  397.  
  398. BOOL TapeLab::fileNew() 
  399. {
  400.     labelformat.~_labelformat();  // azzera la struttura dati e ricreala!
  401.     labelformat._labelformat();      // kill the old label, and make a new one.    
  402.     restoreInitialFormat();
  403.     posy=-1;                // recalculate la scroll bar
  404.     sizey=-1;
  405.     invalidate(TRUE);
  406.     return TRUE;
  407. }
  408.  
  409.  
  410. BOOL TapeLab::filePrint() 
  411. {
  412.     char buf[300];
  413.  
  414.     if (fileFlags==PMMWFF_FILE) sprintf(buf,"Tape Lab Document: %s",filename);
  415.     else sprintf(buf,"Tape Lab Document: %s",fileUntitled);
  416.  
  417.     new PMPrintLabelThread(buf,this);
  418.     
  419.     return TRUE;
  420. }
  421.  
  422. BOOL TapeLab::fileOpen(PCSZ filename) 
  423. {
  424.     if (labelformat.load(filename)) {
  425.         posy=-1;                // recalculate la scroll bar
  426.         sizey=-1;
  427.         invalidate(TRUE);
  428.         return TRUE;
  429.     }
  430.     ErrBox("Error Loading file %s",filename);
  431.  
  432.     return FALSE;
  433. }
  434.  
  435. BOOL TapeLab::fileSave(PCSZ filename) 
  436. {
  437.     labelformat.save(filename);
  438.     invalidate(TRUE);
  439.     return TRUE;
  440. }
  441.  
  442. // if application is quitting, store the window position
  443. void TapeLab::saveApplication()
  444. {
  445.     storeWindowPos(SA_APPLNAME,SA_SWPKEY);
  446. }
  447.  
  448. BOOL TapeLab::create(PMEvent &)
  449. {
  450.     // if possible restore window position. If not possible, set initial position.
  451.     BOOL bSuccess;
  452.     bSuccess=restoreWindowPos(SA_APPLNAME,SA_SWPKEY);
  453.     if (bSuccess) setFramePos(HWND_TOP,0,0,0,0,SWP_ACTIVATE|SWP_SHOW);
  454.     else setFramePos(HWND_TOP,10,10,400,450,SWP_ACTIVATE|SWP_SHOW|SWP_MOVE|SWP_SIZE);
  455.     // if present, load initial format from ini file..
  456.     restoreInitialFormat();
  457.     return TRUE;    
  458. }
  459.  
  460. void TapeLab::restoreInitialFormat()
  461. {
  462.     _labelformat locallabelformat;
  463.        BOOL bSuccess;
  464.        PMUserProfile myprofile;                       // open user profile
  465.     ULONG buffersize = sizeof(labelformat);        // load profile data
  466.     bSuccess = myprofile.queryData(SA_APPLNAME,SA_SETTINGSKEY,&locallabelformat,&buffersize);
  467.     if (bSuccess) {
  468.         // tell labelformat to update selected data fields.
  469.         labelformat.newformat(&locallabelformat);
  470.     }
  471. }
  472.  
  473.  
  474.