home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / exedt040.zip / src / main.c < prev    next >
C/C++ Source or Header  |  1999-01-23  |  12KB  |  394 lines

  1. #define INCL_PM
  2.  
  3. #include <string.h>
  4. #include <os2.h>
  5. #include "exedit.h"
  6.  
  7.  
  8. int main (int argc, char *argv[])
  9. {
  10.  HAB hab;
  11.  HINI Profile;
  12.  ULONG SetSize=sizeof(TSettings);
  13.  TMain *Main;
  14.  TFMain *FMain;
  15.  HMQ hmq;
  16.  QMSG msg;
  17.  ULONG ulCreate;
  18.  HWND hwndFrame,hwndClient;
  19.  HWND hwndToolbar,hwndStatusbar;
  20.  
  21.  hab=WinInitialize(0);
  22.  hmq=WinCreateMsgQueue(hab,0);
  23.  
  24.  
  25.  //Register and Create Main Window
  26.  WinRegisterClass(hab,WC_MAIN,MainWindow_Proc,CS_SIZEREDRAW,sizeof(PVOID)*2);
  27.  ulCreate=FCF_STANDARD & ~FCF_ACCELTABLE;
  28.  hwndFrame=WinCreateStdWindow(HWND_DESKTOP,0,&ulCreate,WC_MAIN,
  29.                               "ExEdit",0,NULLHANDLE,Id_MainWindow,&hwndClient);
  30.  Main=WinQueryWindowPtr(hwndClient,0);
  31.  
  32.  // Load Settings from INI
  33.  Profile=PrfOpenProfile(hab,"exedit.ini");
  34.  PrfQueryProfileData(Profile,"exedit","Settings",&Main->DefSettings,&SetSize);
  35.  // Register child Window
  36.  RegisterChild(hab);
  37.  
  38.  //Create Toolbar and add items
  39.  
  40.  RegisterToolBar(hab);
  41.  hwndToolbar=WinCreateWindow(hwndFrame,WC_TOOL, "ToolBar", WS_VISIBLE,
  42.                 0,0,0,0,hwndFrame,HWND_TOP,Id_Toolbar,NULL,NULL);
  43.  
  44.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_File_New,M_File_New),(MPARAM)"Open a new file");
  45.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_File_Open,M_File_Open),(MPARAM)"Open a file");
  46.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_File_Save,M_File_Save),(MPARAM)"Save current file");
  47.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_File_Print,M_File_Print),(MPARAM)"Print current file");
  48.  WinSendMsg(hwndToolbar,TM_INSERTITEM,(MPARAM)TIT_SEPARATOR,0);
  49.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_Edit_Copy,M_Edit_Copy),(MPARAM)"Copy selected text");
  50.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_Edit_Cut,M_Edit_Cut),(MPARAM)"Cut selected text");
  51.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_Edit_Paste,M_Edit_Paste),(MPARAM)"Paste clipboard text");
  52.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_Edit_Clear,M_Edit_Clear),(MPARAM)"Clear selected text");
  53.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_Edit_Search,M_Edit_Search),(MPARAM)"Search and replace");
  54.  WinSendMsg(hwndToolbar,TM_INSERTITEM,(MPARAM)TIT_SEPARATOR,0);
  55.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_File_Settings,M_File_Settings),(MPARAM)"Change default settings");
  56.  WinSendMsg(hwndToolbar,TM_INSERTITEM,MPFROM2SHORT(M_Help_About,M_Help_About),(MPARAM)"Help");
  57.  
  58.  //Create StatusBar
  59.  RegisterStatusBar(hab);
  60.  hwndStatusbar=WinCreateWindow(hwndFrame,WC_STATUS, "StatusBar", WS_VISIBLE,
  61.                 0,0,0,0,hwndFrame,HWND_TOP,Id_Statusbar,NULL,NULL);
  62.  WinSendMsg(hwndStatusbar,SBM_ADDPANEL,MPFROM2SHORT(30,SB_PERCENT),MPFROM2SHORT(TRUE,39));
  63.  WinSendMsg(hwndStatusbar,SBM_ADDPANEL,MPFROM2SHORT(80,SB_FIX),MPFROM2SHORT(TRUE,40));
  64.  Main->hwndStatusbar=hwndStatusbar;
  65.  
  66.  //Subclass Frame window
  67.  FMain=(TFMain*)malloc(sizeof(TFMain));
  68.  FMain->usSzStruct=sizeof(TFMain);
  69.  FMain->hwndToolbar=hwndToolbar;
  70.  FMain->hwndStatusbar=hwndStatusbar;
  71.  FMain->OldProc=WinSubclassWindow(hwndFrame,FrameWindow_Proc);
  72.  WinSetWindowULong(hwndFrame,QWL_USER,(ULONG)FMain);
  73.  
  74.  //Size and Active Frame Window
  75.  if (WinRestoreWindowPos(APP_NAME,SAVE_WINPOS,hwndFrame))
  76.   WinSetWindowPos(hwndFrame,HWND_TOP,0,0,0,0,SWP_ACTIVATE | SWP_SHOW);
  77.  else
  78.   WinSetWindowPos(hwndFrame,NULLHANDLE,10,10,600,400,SWP_ACTIVATE|SWP_MOVE|SWP_SIZE|SWP_SHOW);
  79.  
  80.  //Disable Child Menu Items
  81.  CItemsEnable(hwndClient,FALSE);
  82.  
  83.  //Command line
  84.  if (argc>1)
  85.   FileOpen(WinQueryWindowPtr(CreateChild(hwndClient,argv[1]),0));
  86.  
  87.  while (WinGetMsg(hab,&msg,NULLHANDLE,0,0))
  88.  WinDispatchMsg(hab,&msg);
  89.  
  90.  PrfWriteProfileData(Profile,"exedit","Settings",&Main->DefSettings,SetSize);
  91.  PrfCloseProfile(Profile);
  92.  
  93.  WinDestroyWindow(hwndFrame);
  94.  DosFreeMem(FMain);
  95.  WinDestroyWindow(hwndStatusbar);
  96.  WinDestroyWindow(hwndToolbar);
  97.  WinDestroyMsgQueue(hmq);
  98.  WinTerminate(hab);
  99.  return 0;
  100. }
  101.  
  102. MRESULT EXPENTRY MainWindow_Proc (HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)
  103. {
  104.  TMain *Main;
  105.  Main=WinQueryWindowPtr(hwnd,0);
  106.  
  107.  switch (msg)
  108.  {
  109.  
  110.   case WM_CREATE:
  111.    Main = (TMain*)malloc(sizeof(TMain));
  112.    WinSetWindowPtr(hwnd,0,Main);
  113.    Main->usSzStruct= sizeof(TMain);
  114.    strcpy(Main->Untitled,"Untitled0");
  115.    memset(&Main->FileDlg, 0, sizeof(FILEDLG));
  116.    Main->FileDlg.cbSize = sizeof(FILEDLG);
  117.    Main->hwndMleMenu=WinLoadMenu(HWND_DESKTOP,NULLHANDLE,M_Mle);
  118.   break;
  119.  
  120.   case DM_DRAGOVER:
  121.   {
  122.    PDRAGINFO pDragInfo=(PDRAGINFO)mp1;
  123.    PDRAGITEM pDragItem;
  124.    int x;
  125.  
  126.    DrgAccessDraginfo(pDragInfo);
  127.    for(x=0;x<pDragInfo->cditem;x++)
  128.    {
  129.     pDragItem=DrgQueryDragitemPtr(pDragInfo,x);
  130.     if (pDragItem->fsControl & DC_CONTAINER || pDragItem->fsControl & DC_REF)
  131.     {
  132.      DrgFreeDraginfo(pDragInfo);
  133.      return MPFROM2SHORT(DOR_NEVERDROP,DO_UNKNOWN);
  134.     }
  135.    }
  136.    DrgFreeDraginfo(pDragInfo);
  137.  
  138.   }
  139.   return MPFROM2SHORT(DOR_DROP,DO_UNKNOWN);
  140.  
  141.   case DM_DROP:
  142.   {
  143.    PDRAGINFO pDragInfo;
  144.    PDRAGITEM pDragItem;
  145.  
  146.    char dragname[CCHMAXPATH];
  147.    int x;
  148.  
  149.    pDragInfo=(PDRAGINFO)mp1;
  150.    DrgAccessDraginfo(pDragInfo);
  151.  
  152.    for (x=0;x<pDragInfo->cditem;x++)
  153.    {
  154.     pDragItem=DrgQueryDragitemPtr(pDragInfo,x);
  155.     DrgQueryStrName(pDragItem->hstrContainerName,CCHMAXPATH,dragname);
  156.     DrgQueryStrName(pDragItem->hstrSourceName,CCHMAXPATH,dragname+strlen(dragname));
  157.  
  158.     FileOpen(WinQueryWindowPtr(CreateChild(hwnd,dragname),0));
  159.    }
  160.    DrgFreeDraginfo(pDragInfo);
  161.  
  162.   }
  163.   return 0;
  164.  
  165.  
  166.   case WM_COMMAND:
  167.   {
  168.    switch (SHORT1FROMMP(mp1))
  169.    {
  170.     //Menu Commands
  171.  
  172.     case M_File_New:
  173.     CreateChild(hwnd,NULL);
  174.     break;
  175.  
  176.     case M_File_Open:
  177.      Main->FileDlg.fl=FDS_OPEN_DIALOG|FDS_CENTER;
  178.      WinFileDlg(HWND_DESKTOP,hwnd, &Main->FileDlg);
  179.      if (Main->FileDlg.lReturn == DID_OK)
  180.      WinSendMsg(CreateChild(hwnd,Main->FileDlg.szFullFile),WM_COMMAND,mp1,0);
  181.     break;
  182.  
  183.     case M_File_Settings:
  184.     {
  185.      TSettingsNB *SettingsNB;
  186.      SettingsNB = (TSettingsNB*)malloc(sizeof(TSettingsNB));
  187.      memcpy(&SettingsNB->Settings,&Main->DefSettings,sizeof(TSettings));
  188.      if(WinDlgBox(HWND_DESKTOP,hwnd,Dlg_Settings_Proc,NULLHANDLE,Id_Settings,(PVOID)SettingsNB)==TRUE)
  189.      {
  190.       memcpy(&Main->DefSettings,&SettingsNB->Settings,sizeof(TSettings));
  191.       UpdateSettings(hwnd,&Main->DefSettings,TRUE);
  192.      }
  193.      DosFreeMem(SettingsNB);
  194.     }
  195.     break;
  196.  
  197.     case M_File_Exit:
  198.      WinSendMsg(hwnd,WM_CLOSE,0,0);
  199.     break;
  200.  
  201.     case M_Window_Cascade:
  202.     {
  203.      SWP Swp;
  204.      HWND hwndChild;
  205.      HENUM henum;
  206.      int x=0,y=0;
  207.  
  208.      MaxChilds(hwnd);
  209.      henum = WinBeginEnumWindows(hwnd);
  210.      hwndChild=WinGetNextWindow(henum);
  211.      WinGetMaxPosition(hwndChild,&Swp);
  212.      do
  213.      {
  214.       WinSetWindowPos(WinQueryWindow(hwndChild,QW_PARENT),HWND_TOP,0,0,0,0,
  215.                       SWP_RESTORE);
  216.       WinSetWindowPos(hwndChild,HWND_TOP,Swp.x+x+4,Swp.y+4,Swp.cx-x-7,Swp.cy-y-7,
  217.                       SWP_SIZE |SWP_MOVE| SWP_NOADJUST |SWP_ZORDER);
  218.       WinSetActiveWindow(HWND_DESKTOP,hwndChild);
  219.       x+=15;
  220.       y+=15;
  221.      }
  222.      while((hwndChild=WinGetNextWindow(henum))!=NULLHANDLE);
  223.      WinEndEnumWindows(henum);
  224.     }
  225.     return 0;
  226.  
  227.     case M_Window_VTile:
  228.     {
  229.      SWP Swp;
  230.      HWND hwndChild;
  231.      HENUM henum;
  232.      int iy,y=0,count=0;
  233.  
  234.      MaxChilds(hwnd);
  235.      count=ChildCount(hwnd);
  236.      if (count)
  237.      {
  238.  
  239.       henum=WinBeginEnumWindows(hwnd);
  240.       hwndChild=WinGetNextWindow(henum);
  241.       WinGetMaxPosition(hwndChild,&Swp);
  242.       Swp.cx-=7;
  243.       Swp.x+=4;
  244.       Swp.cy-=7;
  245.       Swp.y+=4;
  246.       iy=Swp.cy/count;
  247.       do
  248.       {
  249.        WinSetWindowPos(WinQueryWindow(hwndChild,QW_PARENT),HWND_TOP,0,0,0,0,
  250.                       SWP_RESTORE);
  251.        WinSetWindowPos(hwndChild,HWND_TOP,Swp.x,Swp.y+y,Swp.cx,iy,
  252.                        SWP_SIZE |SWP_MOVE| SWP_NOADJUST |SWP_ZORDER);
  253.        y+=iy;
  254.       }
  255.       while((hwndChild=WinGetNextWindow(henum))!=NULLHANDLE);
  256.      }
  257.     }
  258.     return 0;
  259.  
  260.     case M_Window_HTile:
  261.     {
  262.      SWP Swp;
  263.      HWND hwndChild;
  264.      HENUM henum;
  265.      int ix,x=0,count=0;
  266.  
  267.      MaxChilds(hwnd);
  268.      count=ChildCount(hwnd);
  269.      if (count)
  270.      {
  271.  
  272.       henum = WinBeginEnumWindows(hwnd);
  273.       hwndChild=WinGetNextWindow(henum);
  274.       WinGetMaxPosition(hwndChild,&Swp);
  275.       Swp.cx-=7;
  276.       Swp.x+=4;
  277.       Swp.cy-=7;
  278.       Swp.y+=4;
  279.       ix=Swp.cx/count;
  280.       do
  281.       {
  282.        WinSetWindowPos(hwndChild,HWND_TOP,Swp.x+x,Swp.y,ix,Swp.cy,
  283.                        SWP_SIZE |SWP_MOVE| SWP_NOADJUST |SWP_ZORDER);
  284.        x+=ix;
  285.       }
  286.       while((hwndChild=WinGetNextWindow(henum))!=NULLHANDLE);
  287.       WinEndEnumWindows(henum);
  288.      }
  289.     }
  290.     return 0;
  291.  
  292.     case M_Window_Next:
  293.     {
  294.      HWND hwndActive;
  295.      hwndActive=WinQueryActiveWindow(hwnd);
  296.      WinSetActiveWindow(HWND_DESKTOP,WinQueryWindow(hwndActive,QW_NEXT));
  297.      WinSetWindowPos(hwndActive,HWND_BOTTOM,0,0,0,0,SWP_ZORDER|SWP_RESTORE);
  298.     }
  299.     return 0;
  300.  
  301.     case M_Window_CloseAll:
  302.      WinBroadcastMsg(hwnd,WM_CLOSEALL,0,0,BMSG_SEND);
  303.     return 0;
  304.  
  305.     case M_Help_About:
  306.      WinDlgBox(HWND_DESKTOP,hwnd,WinDefDlgProc,NULLHANDLE,DLG_ABOUT,0);
  307.     return 0;
  308.  
  309.     default:
  310.     if (SHORT1FROMMP(mp1)>=WinList_First)
  311.     {
  312.      MENUITEM item;
  313.      WinSendMsg(WinWindowFromID(WinQueryWindow(hwnd,QW_PARENT),FID_MENU),MM_QUERYITEM,MPFROM2SHORT(SHORT1FROMMP(mp1),TRUE),MPFROMP(&item));
  314.      WinSetActiveWindow(HWND_DESKTOP,item.hItem);
  315.      break;
  316.     }
  317.     ChildSendMsg(hwnd,WM_COMMAND,mp1,0);
  318.     return WinDefWindowProc(hwnd,msg,mp1,mp2);
  319.     }
  320.   }
  321.  
  322.   case WM_PAINT:
  323.   {
  324.    HPS hpsPaint;
  325.    RECTL rclPaint;
  326.    hpsPaint=WinBeginPaint(hwnd,NULLHANDLE,&rclPaint);
  327.    WinFillRect(hpsPaint,&rclPaint,CLR_DARKGRAY);
  328.    WinEndPaint(hpsPaint);
  329.   }
  330.   break;
  331.  
  332.   case WM_SAVEAPPLICATION:
  333.    WinStoreWindowPos(APP_NAME,SAVE_WINPOS,WinQueryWindow(hwnd, QW_PARENT));
  334.   break;
  335.  
  336.   case WM_CLOSE:
  337.    WinSendMsg(hwnd,WM_COMMAND,MPFROM2SHORT(M_Window_CloseAll,0),0);
  338.   break;
  339.  
  340.   case WM_DESTROY:
  341.   WinDestroyWindow(Main->hwndMleMenu);
  342.   break;
  343.  
  344.   default:
  345.   return WinDefWindowProc(hwnd,msg,mp1,mp2);
  346.  }
  347.  
  348. return WinDefWindowProc(hwnd,msg,mp1,mp2);
  349. }
  350.  
  351. MRESULT EXPENTRY FrameWindow_Proc(HWND hwnd,ULONG msg, MPARAM mp1,MPARAM mp2)
  352. {
  353.  TFMain *FMain;
  354.  FMain = (TFMain*) WinQueryWindowULong(hwnd,QWL_USER);
  355.  
  356.  switch (msg)
  357.  {
  358.  
  359.   case WM_FORMATFRAME:
  360.   {
  361.    USHORT itemCount = SHORT1FROMMR( FMain->OldProc( hwnd, msg, mp1, mp2 ));
  362.    USHORT Client=itemCount-1;
  363.    USHORT Tool=itemCount;
  364.    USHORT Status=itemCount+1;
  365.  
  366.    PSWP Pswp=PVOIDFROMMP(mp1);
  367.  
  368.    Pswp[Tool].fl = SWP_SIZE | SWP_MOVE;
  369.    Pswp[Tool].cy = TIT_FIRSTY*2+TIT_CXCY;
  370.    Pswp[Tool].cx = Pswp[Client].cx;
  371.    Pswp[Tool].x = Pswp[Client].x;
  372.    Pswp[Tool].y = Pswp[Client].y + Pswp[Client].cy - Pswp[Tool].cy;
  373.    Pswp[Tool].hwndInsertBehind = HWND_TOP ;
  374.    Pswp[Tool].hwnd = FMain->hwndToolbar;
  375.  
  376.    Pswp[Status].fl = SWP_SIZE | SWP_MOVE;
  377.    Pswp[Status].cy = H_Status;
  378.    Pswp[Status].cx = Pswp[Client].cx;
  379.    Pswp[Status].x = Pswp[Client].x;
  380.    Pswp[Status].y = Pswp[Client].y;
  381.    Pswp[Status].hwndInsertBehind = HWND_TOP ;
  382.    Pswp[Status].hwnd = FMain->hwndStatusbar;
  383.  
  384.    Pswp[Client].cy= Pswp[Client].cy - Pswp[Tool].cy-Pswp[Status].cy;
  385.    Pswp[Client].y+= Pswp[Status].cy;
  386.    return( MRFROMSHORT(itemCount+2));
  387.   }
  388.  
  389.   default:
  390.   return FMain->OldProc(hwnd,msg,mp1,mp2);
  391.  }
  392. }
  393.  
  394.