home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / basic / emed15 / samples / owl / mdtchldc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-05  |  13.5 KB  |  441 lines

  1. /*  Project emedit
  2.     Early Morning Software
  3.     Copyright ⌐ 1993. All Rights Reserved.
  4.  
  5.     SUBSYSTEM:    emedit.exe Application
  6.     FILE:         mdtchldc.cpp
  7.     AUTHOR:       Ted Stockwell
  8.  
  9.  
  10.     OVERVIEW
  11.     ========
  12.     Source file for implementation of emeditChildClient (TDialog).
  13. */
  14.  
  15. #include <owl\owlpch.h>
  16. #pragma hdrstop
  17.  
  18. #include "mdtchldc.h"
  19. #include "mdtmssgb.h" // editpad message bar
  20. #include "emedtapp.h" // editpad application
  21. #include "replace.h"
  22. #include "confirm.h"
  23. #include "mdtfnddl.h"
  24.  
  25. emeditReplaceDialogXfer SearchData;
  26.  
  27. //
  28. // Build a response table for all messages/commands handled
  29. // by the application.
  30. //
  31. DEFINE_RESPONSE_TABLE2(emeditChildClient, TDialog, TVbxEventHandler)
  32. //{{emeditChildClientRSP_TBL_BEGIN}}
  33.     EV_WM_SIZE,
  34.     EV_COMMAND_ENABLE(CM_EDITCOPY, CmEditSelectEnable),
  35.     EV_COMMAND_ENABLE(CM_EDITCUT, CmEditSelectEnable),
  36.     EV_COMMAND_ENABLE(CM_EDITDELETE, CmEditSelectEnable),
  37.     EV_COMMAND_ENABLE(CM_EDITWRAP, CmEditSelectEnable),
  38.     EV_COMMAND(CM_EDITCLEAR, CmEditClear),
  39.     EV_COMMAND(CM_EDITCOPY, CmEditCopy),
  40.     EV_COMMAND(CM_EDITCUT, CmEditCut),
  41.     EV_COMMAND(CM_EDITDELETE, CmEditDelete),
  42.     EV_COMMAND(CM_EDITWRAP, CmEditWrap),
  43.     EV_COMMAND_ENABLE(CM_EDITPASTE, CmEditPasteEnable),
  44.     EV_COMMAND(CM_EDITPASTE, CmEditPaste),
  45.     EV_COMMAND(CM_EDITSELECT_ALL, CmEditSelectAll),
  46.     EV_VBXEVENTNAME(IDC_CHILDCLIENTEDITOR, "CaretChange", CaretChange),
  47.     EV_VBXEVENTNAME(IDC_CHILDCLIENTEDITOR, "Change", Change),
  48.     EV_COMMAND_ENABLE(CM_EDITUNDO, CmEditUndoEnable),
  49.     EV_COMMAND_ENABLE(CM_EDITREDO, CmEditRedoEnable),
  50.     EV_COMMAND(CM_EDITREDO, CmEditRedo),
  51.     EV_COMMAND(CM_EDITUNDO, CmEditUndo),
  52.     EV_COMMAND_ENABLE(CM_EDITFIND, CmEditFindEnable),
  53.     EV_COMMAND(CM_EDITFIND, CmEditFind),
  54.     EV_COMMAND(CM_EDITFINDNEXT, CmEditFindNext),
  55.     EV_COMMAND_ENABLE(CM_EDITFINDNEXT, CmEditFindNextEnable),
  56.     EV_COMMAND(CM_EDITREPLACE, CmEditReplace),
  57.     EV_COMMAND_ENABLE(CM_EDITREPLACE, CmEditReplaceEnable),
  58.     EV_VBXEVENTNAME(IDC_CHILDCLIENTEDITOR, "SearchReplaceConfirm", SearchReplaceConfirm),
  59. //{{emeditChildClientRSP_TBL_END}}
  60. END_RESPONSE_TABLE;
  61.  
  62.  
  63. //{{emeditChildClient Implementation}}
  64.  
  65.  
  66. emeditChildClient::emeditChildClient (TWindow* parent, TResId resId, TModule* module):
  67.     TDialog(parent, resId, module), TWindow( parent )
  68. {
  69.   editor= new emeditEditor( this, IDC_CHILDCLIENTEDITOR );
  70. }
  71.  
  72.  
  73. emeditChildClient::~emeditChildClient ()
  74. {
  75.     Destroy();
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. void emeditChildClient::EvSize (UINT sizeType, TSize& size)
  83. {
  84.     TDialog::EvSize(sizeType, size);
  85.  
  86.     // Size the editor control to fit the client window.
  87.     editor->SetPropTop( 0 );
  88.     editor->SetPropLeft( 0 );
  89.     editor->SetPropWidth( size.cx );
  90.     editor->SetPropHeight( size.cy );
  91. }
  92.  
  93.  
  94.  
  95. void emeditChildClient::CmEditSelectEnable (TCommandEnabler &tce)
  96. {
  97.   ENUM SelType;
  98.   editor->GetPropSelMark( SelType );
  99.   tce.Enable( SelType != TVbxEditor::SelMark_0_None );
  100. }
  101.  
  102.  
  103. void emeditChildClient::CmEditClear ()
  104. {
  105.   editor->SetPropRedraw( FALSE );
  106.     CmEditSelectAll();
  107.   CmEditDelete();
  108.   editor->SetPropRedraw( TRUE );
  109. }
  110.  
  111.  
  112. void emeditChildClient::CmEditCopy ()
  113. {
  114.   editor->SetPropAction( TVbxEditor::Action_1_Copy );
  115. }
  116.  
  117.  
  118. void emeditChildClient::CmEditCut ()
  119. {
  120.   editor->SetPropAction( TVbxEditor::Action_3_Cut );
  121. }
  122.  
  123.  
  124. void emeditChildClient::CmEditDelete ()
  125. {
  126.   editor->SetPropAction( TVbxEditor::Action_4_Clear );
  127. }
  128.  
  129.  
  130. void emeditChildClient::CmEditWrap ()
  131. {
  132.   long width= 0;
  133.  
  134.   editor->GetPropWidth( width ); // get the width of the window, in pixels
  135.   editor->SetPropWrapY( (int)width ); // get the wrap column to the width of window
  136.   editor->SetPropWrapWholeWords( TRUE ); // wrap whole words
  137.   editor->SetPropAction( TVbxEditor::Action_5_Wrap ); // wrap the text
  138. }
  139.  
  140.  
  141. void emeditChildClient::CmEditPasteEnable (TCommandEnabler &tce)
  142. {
  143.   TClipboard& clipboard = OpenClipboard();
  144.   tce.Enable(clipboard.IsClipboardFormatAvailable(CF_TEXT));
  145.   clipboard.CloseClipboard();
  146. }
  147.  
  148.  
  149. void emeditChildClient::CmEditPaste ()
  150. {
  151.   editor->SetPropAction( TVbxEditor::Action_2_Paste );
  152. }
  153.  
  154.  
  155. void emeditChildClient::CmEditSelectAll ()
  156. {
  157.   // Implement code equivalent to the following VB code :
  158.   // Editor1.Redraw= False
  159.   // SendKeys( "^{HOME}", TRUE ); 'move caret to top of file
  160.   // SendKeys( "+^{END}", TRUE ); ' mark entire file
  161.   // Editor1.Redraw= True
  162.   BYTE pbOrigKeyState[256], pbNewKeyState[256];
  163.  
  164.   editor->SetPropRedraw( FALSE );
  165.  
  166.   GetKeyboardState((LPBYTE) &pbOrigKeyState); // save keyboard state
  167.   memcpy( pbNewKeyState, pbOrigKeyState, sizeof( pbNewKeyState ) );
  168.  
  169.   // Trick receiving window into thinking the Ctrl key is down.
  170.   // For some receiving windows other than EMEDIT it might also be necessary
  171.   // to properly set the lparam of the message that we send but it's not
  172.   // necessary for EMEDIT.
  173.   pbNewKeyState[VK_CONTROL] |= 0x80;
  174.   SetKeyboardState((LPBYTE) &pbNewKeyState);
  175.   ::SendMessage( editor->HWindow, WM_KEYDOWN, VK_HOME, 0 );
  176.  
  177.   pbNewKeyState[VK_SHIFT] |= 0x80;
  178.   SetKeyboardState((LPBYTE) &pbNewKeyState);
  179.   ::SendMessage( editor->HWindow, WM_KEYDOWN, VK_END, 0 );
  180.  
  181.   SetKeyboardState((LPBYTE) &pbOrigKeyState); // restore keyboard state
  182.  
  183.   editor->SetPropRedraw( TRUE );
  184.  
  185.   // NOTE : I didn't have to implement this the way I did above, I just thought
  186.   // it would be cool to reproduce the way I originally did it in Visual Basic
  187.   // (using SendKeys, a routine not available with C++).  I could have
  188.   // implemented this more straightforwardlly as shown below.  The method
  189.   // above of simulating SendKey could be usful if I wanted to do something like
  190.   // move to the beginning of the next word, then I would "send"  Ctrl-Right
  191.   // Arrow.
  192. /*
  193.   editor->SetPropRedraw( FALSE ); // don't redraw window til done
  194.   editor->SetPropCaretY(1); // move to the first line
  195.   editor->SetPropCaretX(1); // move to the 1st column in the 1st line
  196.   editor->SetPropSelMark( emeditEditor::SelMark_2_Line ); // start a line block
  197.  
  198.   long count;
  199.   string lastline;
  200.   editor->GetPropCount( count );     // get # of lines in the file
  201.   editor->SetPropTextIndex( count ); // get the last
  202.   editor->GetPropText( lastline );   //   line in the file
  203.   editor->SetPropCaretY( count );        // move caret to last line in file
  204.   editor->SetPropCaretX( lastline.length() ); // move caret to end of last line
  205.   editor->SetPropRedraw( TRUE ); // now redraw window
  206. */
  207. }
  208.  
  209.  
  210.  
  211. void emeditChildClient::CaretChange (VBXEVENT FAR */*event*/) { UpdateStatusBar(); }
  212.  
  213.  
  214. void emeditChildClient::Change (VBXEVENT FAR */*event*/) { UpdateStatusBar(); }
  215.  
  216.  
  217. void emeditChildClient::UpdateStatusBar() {
  218.     // INSERT>> Your code here.
  219.   BOOL D;
  220.   long X, Y, C;
  221.   editor->GetPropCaretY( Y );
  222.   editor->GetPropCaretX( X );
  223.   editor->GetPropCount( C );
  224.   editor->GetPropIsDirty( D );
  225.   emeditMessageBar* StatusBar= ((emeditApp*)GetApplication())->StatusBar();
  226.   StatusBar->SetPositionIndicator( X, Y, C );
  227.   StatusBar->SetModifyIndicator( D );
  228. }
  229.  
  230.  
  231. void emeditChildClient::CmEditUndoEnable (TCommandEnabler &tce)
  232. {
  233.   BOOL canundo;
  234.   editor->GetPropCanUndo( canundo );
  235.   tce.Enable( canundo );
  236. }
  237.  
  238.  
  239. void emeditChildClient::CmEditRedoEnable (TCommandEnabler &tce)
  240. {
  241.   BOOL canundo;
  242.   editor->GetPropCanRedo( canundo );
  243.   tce.Enable( canundo );
  244. }
  245.  
  246.  
  247. void emeditChildClient::CmEditRedo ()
  248. {
  249.   editor->SetPropAction( TVbxEditor::Action_7_Redo );
  250. }
  251.  
  252.  
  253.  
  254. void emeditChildClient::CmEditUndo ()
  255. {
  256.   editor->SetPropAction( TVbxEditor::Action_6_Undo );
  257. }
  258.  
  259.  
  260.  
  261.  
  262. // Overiding this method as shown below enables the dialog to process
  263. // accelerator keys.
  264. BOOL emeditChildClient::PreProcessMsg (MSG& msg)
  265. {
  266.     BOOL result= GetApplication()->GetMainWindow()->PreProcessMsg( msg );
  267.     if (!result)
  268.       result = TDialog::PreProcessMsg(msg);
  269.     return result;
  270. }
  271.  
  272.  
  273. void emeditChildClient::CmEditFindEnable (TCommandEnabler &tce) { tce.Enable(TRUE); }
  274.  
  275.  
  276. void emeditChildClient::CmEditFind ()
  277. {
  278.   emeditFindDialog FindDialog( this );
  279.  
  280.   FindDialog.SetTransferBuffer( &SearchData );
  281.  
  282.   // if there's highlighted text that spans one and only one line then stuff
  283.   // the text into the combo box.
  284.   const char* target= GetHilitedString();
  285.   if (target)
  286.     SearchData.SearchText.SelectString( target );
  287.  
  288.   if (FindDialog.Execute() == IDOK) {
  289.  
  290.     // if a target was entered then
  291.     //   add the target to the string list in the transfer buffer
  292.     string& target= SearchData.SearchText.GetSelection();
  293.     if (target.length())
  294.       SearchData.SearchText.AddString( target.c_str() );
  295.  
  296.     // do the search...
  297.     SetSearchProperties( SearchData ); // set control search properties
  298.     editor->SetPropAction( TVbxEditor::Action_9_Search ); // find the string
  299.  
  300.     // if the test was not found then display a message...
  301.     ENUM SearchResult;
  302.     editor->GetPropSearchResult( SearchResult );
  303.     if (SearchResult == TVbxEditor::SearchResult_0_Failure) { // if text was not found
  304.       string Msg= "Cannot find ";
  305.       ((Msg+= "\"")+= target)+= "\"";
  306.       MessageBox( Msg.c_str(), GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK );
  307.     }
  308.   }
  309. }
  310.  
  311.  
  312.  
  313. void emeditChildClient::SetSearchProperties(emeditReplaceDialogXfer& SearchData) {
  314.   editor->SetPropSearchTarget( SearchData.SearchText.GetSelection() );
  315.   editor->SetPropSearchReplacement( SearchData.ReplaceText.GetSelection() );
  316.   editor->SetPropSearchCaseSensitive( SearchData.MatchCase );
  317.   editor->SetPropSearchWholeWordsOnly( SearchData.WholeWordsOnly );
  318.   editor->SetPropSearchTo( SearchData.DirectionUp ?
  319.                            TVbxEditor::SearchTo_0_Top_of_text :
  320.                            TVbxEditor::SearchTo_1_Bottom_of_text );
  321.   editor->SetPropSearchOrigin( SearchData.SearchFromCursor ?
  322.                                TVbxEditor::SearchOrigin_0_From_Cursor :
  323.                                TVbxEditor::SearchOrigin_1_Entire_Scope );
  324. }
  325.  
  326.  
  327.  
  328. const char* emeditChildClient::GetHilitedString() {
  329.   const char* histring= NULL;
  330.  
  331.   ENUM selmark= 0;
  332.   editor->GetPropSelMark( selmark );
  333.   if (selmark != 0 ) { // if there is selected text
  334.     long starty, endy;
  335.     editor->GetPropSelStartY( starty );
  336.     editor->GetPropSelEndY( endy );
  337.     if (starty == endy ) { // if the text selection only spans one line
  338.       long startx, endx;
  339.       editor->GetPropSelStartX( startx );
  340.       editor->GetPropSelEndX( endx );
  341.       if (startx < endx) {
  342.         static string search;
  343.         editor->SetPropTextIndex( starty );
  344.         editor->GetPropText( search );
  345.  
  346.         search= search.substr( (unsigned)startx-1, (unsigned)(endx-startx) ).strip();
  347.         histring= search.c_str();
  348.       }
  349.     }
  350.   }
  351.  
  352.   return histring;
  353. }
  354.  
  355.  
  356. void emeditChildClient::CmEditFindNext ()
  357. {
  358.   string SearchTarget;
  359.   editor->GetPropSearchTarget( SearchTarget );
  360.   if (SearchTarget.length() <= 0) {
  361.     CmEditFind();
  362.   } else {
  363.     editor->SetPropAction( TVbxEditor::Action_11_Search_Again ); // find the string
  364.  
  365.     // if the test was not found then display a message...
  366.     ENUM SearchResult;
  367.     editor->GetPropSearchResult( SearchResult );
  368.     if (SearchResult == TVbxEditor::SearchResult_0_Failure) { // if text was not found
  369.       string Msg= "Cannot find ";
  370.       string target;
  371.       editor->GetPropSearchTarget( target );
  372.       ((Msg+= "\"")+= target)+= "\"";
  373.       MessageBox( Msg.c_str(), GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK );
  374.     }
  375.   }
  376. }
  377.  
  378.  
  379. void emeditChildClient::CmEditFindNextEnable (TCommandEnabler &tce) { tce.Enable(TRUE); }
  380.  
  381.  
  382. void emeditChildClient::CmEditReplace ()
  383. {
  384.   emeditReplaceDialog ReplaceDialog( this );
  385.   ReplaceDialog.SetTransferBuffer( &SearchData );
  386.  
  387.   // if there's highlighted text that spans one and only one line then stuff
  388.   // the text into the combo box.
  389.   const char* target= GetHilitedString();
  390.   if (target)
  391.     SearchData.SearchText.SelectString( target );
  392.  
  393.   if (ReplaceDialog.Execute() == IDOK) {
  394.  
  395.     // if a target was entered then
  396.     //   add the target to the string list in the transfer buffer
  397.     string& target= SearchData.SearchText.GetSelection();
  398.     if (target.length())
  399.       SearchData.SearchText.AddString( target.c_str() );
  400.  
  401.     // if a replacement was entered then
  402.     //   add the replacement to the string list in the transfer buffer
  403.     string& replacement= SearchData.ReplaceText.GetSelection();
  404.     if (replacement.length())
  405.       SearchData.ReplaceText.AddString( replacement.c_str() );
  406.  
  407.  
  408.     // do the search...
  409.     SetSearchProperties( SearchData ); // set control search properties
  410.     editor->SetPropAction( TVbxEditor::Action_10_Replace ); // find the string
  411.  
  412.     // if the test was not found then display a message...
  413.     ENUM SearchResult;
  414.     editor->GetPropSearchResult( SearchResult );
  415.     if (SearchResult == TVbxEditor::SearchResult_0_Failure) { // if text was not found
  416.       string Msg= "Cannot find ";
  417.       ((Msg+= "\"")+= target)+= "\"";
  418.       MessageBox( Msg.c_str(), GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK );
  419.     }
  420.   }
  421. }
  422.  
  423.  
  424. void emeditChildClient::CmEditReplaceEnable (TCommandEnabler &tce) { tce.Enable(TRUE); }
  425.  
  426.  
  427. void emeditChildClient::SearchReplaceConfirm (VBXEVENT FAR *event)
  428. {
  429.   // Confirmation as Integer
  430.   int far * confirmation= *( int far * far *)(event->ParamList);
  431.   emeditConfirmDialog ConfirmDialog( this );
  432.   switch (ConfirmDialog.Execute()) {
  433.     case IDC_YESBUTTON: *confirmation= 1; break;
  434.     case IDC_NOBUTTON:  *confirmation= 2; break;
  435.     case IDC_ONEBUTTON:  *confirmation= 3; break;
  436.     case IDC_ALLBUTTON:  *confirmation= 4; break;
  437.     default:  *confirmation= 0; break;
  438.   }
  439. }
  440.  
  441.