home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 21.ddi / LAYOUT.PAK / LAYDIA.CPP next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  11.9 KB  |  361 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993 by Borland International
  3. //----------------------------------------------------------------------------
  4.  
  5. //
  6. // This is an example of how to use TRACE macros in a program without 
  7. // requiring an OWL diagnostic build.  
  8. // Just define __TRACE (and/or __WARN) before any OWL headers
  9. // Then you can use TRACE() or WARN() directly
  10. //
  11. #define __TRACE
  12.  
  13. #include <owl\owlpch.h>
  14. #include <owl\applicat.h>
  15. #include <owl\layoutwi.h>
  16. #include <owl\layoutco.h>
  17. #include <owl\dialog.h>
  18. #include <owl\combobox.h>
  19. #include <owl\edit.h>
  20. #include <string.h>
  21. #include "layout.rh"
  22. #include "laydia.h"
  23.  
  24.  
  25. DEFINE_RESPONSE_TABLE1(TLayoutDialog, TDialog)
  26.   EV_COMMAND(ID_LAYOUT, HandleLayout),
  27.   EV_LBN_SELCHANGE(ID_WINDOWLIST, HandleWindowChange),
  28. END_RESPONSE_TABLE;
  29.  
  30. TLayoutDialog::TLayoutDialog(TLayoutWindow* parent,
  31.                              TResId         resId,
  32.                              TChildInfo*    childInfo)
  33.   : TDialog(parent, resId),
  34.     LayoutWindow(parent),
  35.     ChildInfo(childInfo)
  36. {
  37.   WindowList     = new TListBox(this, ID_WINDOWLIST);
  38.   
  39.   X1EdgeCombo = new TComboBox(this,ID_X1_MYEDGE);
  40.   X2EdgeCombo = new TComboBox(this,ID_X2_MYEDGE);
  41.   Y1EdgeCombo = new TComboBox(this,ID_Y1_MYEDGE);
  42.   Y2EdgeCombo = new TComboBox(this,ID_Y2_MYEDGE);
  43.  
  44.   X1OtherEdgeCombo = new TComboBox(this,ID_X1_OTHEREDGE);
  45.   X2OtherEdgeCombo = new TComboBox(this,ID_X2_OTHEREDGE);
  46.   Y1OtherEdgeCombo = new TComboBox(this,ID_Y1_OTHEREDGE);
  47.   Y2OtherEdgeCombo = new TComboBox(this,ID_Y2_OTHEREDGE);
  48.  
  49.   X1UnitsCombo     = new TComboBox(this, ID_X1_UNITS);
  50.   X2UnitsCombo     = new TComboBox(this, ID_X2_UNITS);
  51.   Y1UnitsCombo     = new TComboBox(this, ID_Y1_UNITS);
  52.   Y2UnitsCombo     = new TComboBox(this, ID_Y2_UNITS);
  53.  
  54.   X1ValueEdit = new TEdit(this,ID_X1_VALUE);
  55.   X2ValueEdit = new TEdit(this,ID_X2_VALUE);
  56.   Y1ValueEdit = new TEdit(this,ID_Y1_VALUE);
  57.   Y2ValueEdit = new TEdit(this,ID_Y2_VALUE);
  58.   
  59.   X1RelationCombo  = new TComboBox(this, ID_X1_RELATIONSHIP);
  60.   X2RelationCombo  = new TComboBox(this, ID_X2_RELATIONSHIP);
  61.   Y1RelationCombo  = new TComboBox(this, ID_Y1_RELATIONSHIP);
  62.   Y2RelationCombo  = new TComboBox(this, ID_Y2_RELATIONSHIP);
  63.  
  64.   X1RelWinCombo    = new TComboBox(this, ID_X1_RELWIN);
  65.   X2RelWinCombo    = new TComboBox(this, ID_X2_RELWIN);
  66.   Y1RelWinCombo    = new TComboBox(this, ID_Y1_RELWIN);
  67.   Y2RelWinCombo    = new TComboBox(this, ID_Y2_RELWIN);
  68. }
  69.  
  70.  
  71. struct TEnumPair {
  72.   char far* Name;
  73.   DWORD     Value;
  74. };
  75. #define ENUMPAIR(enum)   {#enum, enum}
  76.  
  77.  
  78. //
  79. // For a valid constraint....
  80. // X2Edge must not be the same as X1 edge
  81. // Y2Edge must not be the same as Y1 edge
  82. // If X2Edge == lmWidth then lmLeft and lmRight relations don't make sense
  83. // if Y2Edge == lmHeight then lmAbove and lmBelow relations don't make sense
  84. //
  85.  
  86. TEnumPair X1EdgePairs[] = {
  87.   ENUMPAIR(lmLeft),
  88.   ENUMPAIR(lmCenter),
  89.   ENUMPAIR(lmRight),
  90. };
  91.  
  92. TEnumPair X2EdgePairs[] = {
  93.   ENUMPAIR(lmCenter),
  94.   ENUMPAIR(lmRight),
  95.   ENUMPAIR(lmWidth),
  96. };
  97.  
  98. TEnumPair Y1EdgePairs[] = {
  99.   ENUMPAIR(lmTop),
  100.   ENUMPAIR(lmCenter),
  101.   ENUMPAIR(lmBottom),
  102. };
  103.  
  104. TEnumPair Y2EdgePairs[] = {
  105.   ENUMPAIR(lmCenter),
  106.   ENUMPAIR(lmBottom),
  107.   ENUMPAIR(lmHeight),
  108. };
  109.  
  110. TEnumPair AllEdgePairs[] = {
  111.   ENUMPAIR(lmLeft),
  112.   ENUMPAIR(lmCenter),
  113.   ENUMPAIR(lmRight),
  114.   ENUMPAIR(lmTop),
  115.   ENUMPAIR(lmBottom),
  116.   ENUMPAIR(lmWidth),
  117.   ENUMPAIR(lmHeight),
  118. };
  119.  
  120.  
  121. // Relations for all X edges
  122. TEnumPair XRelationPairs[] = {
  123.   ENUMPAIR(lmAsIs),
  124.   ENUMPAIR(lmAbsolute),
  125.   ENUMPAIR(lmPercentOf),
  126.   ENUMPAIR(lmSameAs),
  127.   ENUMPAIR(lmLeftOf),     //These should be disabled for lmWidth
  128.   ENUMPAIR(lmRightOf),
  129. };
  130.  
  131. TEnumPair YRelationPairs[] = {
  132.   ENUMPAIR(lmAsIs),
  133.   ENUMPAIR(lmAbsolute),
  134.   ENUMPAIR(lmPercentOf),
  135.   ENUMPAIR(lmSameAs),
  136.   ENUMPAIR(lmAbove),      //These should be disabled for lmHeight
  137.   ENUMPAIR(lmBelow),
  138. };
  139.  
  140. TEnumPair RelWinPairs[MaxWindows] = {
  141.   ENUMPAIR(lmParent),
  142. };
  143.  
  144. TEnumPair UnitsPairs[] = {
  145.   ENUMPAIR(lmPixels),
  146.   ENUMPAIR(lmLayoutUnits),
  147. };
  148.  
  149. static void FillCombo(TComboBox* combo, TEnumPair* pairs, int count)
  150. {
  151.   while (combo->DeleteString(0) > 0)
  152.     ;
  153.   for (int i = 0; i < count; i++) {
  154.     int index = combo->AddString(pairs[i].Name);
  155.     combo->SetItemData(index, pairs[i].Value);
  156.   }
  157. }
  158.  
  159. void TLayoutDialog::SetupWindow()
  160. {
  161.   TDialog::SetupWindow();
  162.  
  163.  
  164.   // Fill the window list
  165.   for (int i = 0; ChildInfo[i].Child && i < MaxWindows-1; i++) {
  166.     WindowList->AddString(ChildInfo[i].Child->Title);
  167.     RelWinPairs[i+1].Name = ChildInfo[i].Child->Title;
  168.     RelWinPairs[i+1].Value = DWORD(ChildInfo[i].Child);
  169.   }
  170.  
  171.   WindowList->SetSelIndex(0);
  172.  
  173.   FillCombo(X1EdgeCombo,X1EdgePairs,COUNTOF(X1EdgePairs));
  174.   FillCombo(X2EdgeCombo,X2EdgePairs,COUNTOF(X2EdgePairs));
  175.   FillCombo(Y1EdgeCombo,Y1EdgePairs,COUNTOF(Y1EdgePairs));
  176.   FillCombo(Y2EdgeCombo,Y2EdgePairs,COUNTOF(Y2EdgePairs));
  177.  
  178.   FillCombo(X1OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  179.   FillCombo(X2OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  180.   FillCombo(Y1OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  181.   FillCombo(Y2OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  182.  
  183.   FillCombo(X1UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  184.   FillCombo(X2UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  185.   FillCombo(Y1UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  186.   FillCombo(Y2UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  187.   
  188.   X1ValueEdit->SetText("");
  189.   X2ValueEdit->SetText("");
  190.   Y1ValueEdit->SetText("");
  191.   Y2ValueEdit->SetText("");
  192.  
  193.   FillCombo(X1RelationCombo, XRelationPairs, COUNTOF(XRelationPairs));
  194.   FillCombo(X2RelationCombo, XRelationPairs, COUNTOF(XRelationPairs));
  195.   FillCombo(Y1RelationCombo, YRelationPairs, COUNTOF(YRelationPairs));
  196.   FillCombo(Y2RelationCombo, YRelationPairs, COUNTOF(YRelationPairs));
  197.  
  198.   FillCombo(X1RelWinCombo, RelWinPairs, i+1);
  199.   FillCombo(X2RelWinCombo, RelWinPairs, i+1);
  200.   FillCombo(Y1RelWinCombo, RelWinPairs, i+1);
  201.   FillCombo(Y2RelWinCombo, RelWinPairs, i+1);
  202.  
  203.   ChildNum = -1;  //Flag to signal initial call to HandleWindowChange
  204.   HandleWindowChange();
  205. }
  206.  
  207. static void SelectItemData(TComboBox* combo, DWORD itemData)
  208. {
  209.   int count = combo->GetCount();
  210.   combo->SetSelIndex(-1); //Clear the selection
  211.   for (int i = 0; i < count; i++)
  212.     if (combo->GetItemData(i) == itemData) {
  213.       combo->SetSelIndex(i);
  214.       return;
  215.     }
  216.   TRACE("Couldn't find lmXxx (" << itemData <<") in list");
  217. }
  218.  
  219. //
  220. // Take a given layout constraint & shove it into the dialog
  221. //
  222. void TLayoutDialog::SetLayoutConstraint(TLayoutConstraint& lc, Constraint which)
  223. {
  224.   char buff[20];
  225.  
  226.   switch (which) {
  227.     case X1:
  228.       SelectItemData(X1EdgeCombo, lc.MyEdge);
  229.       SelectItemData(X1RelationCombo, lc.Relationship);
  230.       SelectItemData(X1RelWinCombo, DWORD(lc.RelWin));
  231.       SelectItemData(X1OtherEdgeCombo, lc.OtherEdge);
  232.       SelectItemData(X1UnitsCombo, lc.Units);
  233.       wsprintf(buff, "%d", lc.Value);
  234.       X1ValueEdit->SetText(buff);
  235.       break;
  236.  
  237.     case X2:
  238.       SelectItemData(X2EdgeCombo, lc.MyEdge);
  239.       SelectItemData(X2RelationCombo, lc.Relationship);
  240.       SelectItemData(X2RelWinCombo, DWORD(lc.RelWin));
  241.       SelectItemData(X2OtherEdgeCombo, lc.OtherEdge);
  242.       SelectItemData(X2UnitsCombo, lc.Units);
  243.       wsprintf(buff, "%d", lc.Value);
  244.       X2ValueEdit->SetText(buff);
  245.       break;
  246.  
  247.     case Y1:
  248.       SelectItemData(Y1EdgeCombo, lc.MyEdge);
  249.       SelectItemData(Y1RelationCombo, lc.Relationship);
  250.       SelectItemData(Y1RelWinCombo, DWORD(lc.RelWin));
  251.       SelectItemData(Y1OtherEdgeCombo, lc.OtherEdge);
  252.       SelectItemData(Y1UnitsCombo, lc.Units);
  253.       wsprintf(buff, "%d", lc.Value);
  254.       Y1ValueEdit->SetText(buff);
  255.       break;
  256.  
  257.     case Y2:
  258.       SelectItemData(Y2EdgeCombo, lc.MyEdge);
  259.       SelectItemData(Y2RelationCombo, lc.Relationship);
  260.       SelectItemData(Y2RelWinCombo, DWORD(lc.RelWin));
  261.       SelectItemData(Y2OtherEdgeCombo, lc.OtherEdge);
  262.       SelectItemData(Y2UnitsCombo, lc.Units);
  263.       wsprintf(buff, "%d", lc.Value);
  264.       Y2ValueEdit->SetText(buff);
  265.       break;
  266.   }
  267. }
  268.  
  269. //
  270. // Retrieve a layout constraint from the dialog
  271. //
  272. void TLayoutDialog::GetLayoutConstraint(TLayoutConstraint& lc, Constraint which)
  273. {
  274.   char buff[20];
  275.   switch (which) {
  276.     case X1:
  277.       lc.MyEdge = (TEdge)X1EdgeCombo->GetItemData(X1EdgeCombo->GetSelIndex());
  278.       lc.Relationship = (TRelationship)X1RelationCombo->GetItemData(X1RelationCombo->GetSelIndex());
  279.       lc.RelWin = (TWindow*)X1RelWinCombo->GetItemData(X1RelWinCombo->GetSelIndex());
  280.       lc.OtherEdge = (TEdge)X1OtherEdgeCombo->GetItemData(X1OtherEdgeCombo->GetSelIndex());
  281.       lc.Units = (TMeasurementUnits)X1UnitsCombo->GetItemData(X1UnitsCombo->GetSelIndex());
  282.       X1ValueEdit->GetText(buff, sizeof buff);
  283.       lc.Value = atoi(buff);
  284.       break;
  285.  
  286.     case X2:
  287.       lc.MyEdge = (TEdge)X2EdgeCombo->GetItemData(X2EdgeCombo->GetSelIndex());
  288.       lc.Relationship = (TRelationship)X2RelationCombo->GetItemData(X2RelationCombo->GetSelIndex());
  289.       lc.RelWin = (TWindow*)X2RelWinCombo->GetItemData(X2RelWinCombo->GetSelIndex());
  290.       lc.OtherEdge = (TEdge)X2OtherEdgeCombo->GetItemData(X2OtherEdgeCombo->GetSelIndex());
  291.       lc.Units = (TMeasurementUnits)X2UnitsCombo->GetItemData(X2UnitsCombo->GetSelIndex());
  292.       X2ValueEdit->GetText(buff, sizeof buff);
  293.       lc.Value = atoi(buff);
  294.       break;
  295.  
  296.     case Y1:
  297.       lc.MyEdge = (TEdge)Y1EdgeCombo->GetItemData(Y1EdgeCombo->GetSelIndex());
  298.       lc.Relationship = (TRelationship)Y1RelationCombo->GetItemData(Y1RelationCombo->GetSelIndex());
  299.       lc.RelWin = (TWindow*)Y1RelWinCombo->GetItemData(Y1RelWinCombo->GetSelIndex());
  300.       lc.OtherEdge = (TEdge)Y1OtherEdgeCombo->GetItemData(Y1OtherEdgeCombo->GetSelIndex());
  301.       lc.Units = (TMeasurementUnits)Y1UnitsCombo->GetItemData(Y1UnitsCombo->GetSelIndex());
  302.       Y1ValueEdit->GetText(buff, sizeof buff);
  303.       lc.Value = atoi(buff);
  304.       break;
  305.  
  306.     case Y2:
  307.       lc.MyEdge = (TEdge)Y2EdgeCombo->GetItemData(Y2EdgeCombo->GetSelIndex());
  308.       lc.Relationship = (TRelationship)Y2RelationCombo->GetItemData(Y2RelationCombo->GetSelIndex());
  309.       lc.RelWin = (TWindow*)Y2RelWinCombo->GetItemData(Y2RelWinCombo->GetSelIndex());
  310.       lc.OtherEdge = (TEdge)Y2OtherEdgeCombo->GetItemData(Y2OtherEdgeCombo->GetSelIndex());
  311.       lc.Units = (TMeasurementUnits)Y2UnitsCombo->GetItemData(Y2UnitsCombo->GetSelIndex());
  312.       Y2ValueEdit->GetText(buff, sizeof buff);
  313.       lc.Value = atoi(buff);
  314.       break;
  315.  
  316.   }
  317. }
  318.  
  319. // Window changed
  320. // Ask user to save current layout constraints?
  321.  
  322. void TLayoutDialog::HandleWindowChange()
  323. {
  324.   if (ChildNum>=0) {
  325.     // Save all of the current settings, unless this is the first call
  326.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.X,X1);
  327.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Width,X2);
  328.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Y,Y1);
  329.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Height,Y2);
  330.   }
  331.   // Switch to new window
  332.   ChildNum = WindowList->GetSelIndex();
  333.   SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.X,X1);
  334.   SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Width,X2);
  335.   SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Y,Y1);
  336.   SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Height,Y2);
  337. }
  338.  
  339. // Get the constraints from each window, update child window
  340. void TLayoutDialog::HandleLayout()
  341. {
  342.   TLayoutMetrics& lm = ChildInfo[ChildNum].LayoutMetrics;
  343.  
  344.   GetLayoutConstraint(lm.X,X1);
  345.   GetLayoutConstraint(lm.Width,X2);
  346.   GetLayoutConstraint(lm.Y,Y1);
  347.   GetLayoutConstraint(lm.Height,Y2);
  348.  
  349.   // Check some basic restrictions
  350.   if (lm.X.MyEdge == lm.Width.MyEdge) {
  351.       MessageBox("X1.MyEdge can not be the same as X2.MyEdge");
  352.       return;
  353.       }
  354.   if (lm.Y.MyEdge == lm.Height.MyEdge) {
  355.       MessageBox("Y1.MyEdge can not be the same as Y2.MyEdge");
  356.       return;
  357.       }
  358.  
  359.   Parent->PostMessage(WM_COMMAND, CM_RELAYOUT);
  360. }
  361.