home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / layout.pak / LAYDIA.CPP next >
C/C++ Source or Header  |  1997-07-23  |  13KB  |  382 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993,1994 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.   uint32    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.   // Fill the window list
  164.   //
  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 = uint32(ChildInfo[i].Child);
  169.   }
  170.  
  171.   FillCombo(X1EdgeCombo,X1EdgePairs,COUNTOF(X1EdgePairs));
  172.   FillCombo(X2EdgeCombo,X2EdgePairs,COUNTOF(X2EdgePairs));
  173.   FillCombo(Y1EdgeCombo,Y1EdgePairs,COUNTOF(Y1EdgePairs));
  174.   FillCombo(Y2EdgeCombo,Y2EdgePairs,COUNTOF(Y2EdgePairs));
  175.  
  176.   FillCombo(X1OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  177.   FillCombo(X2OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  178.   FillCombo(Y1OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  179.   FillCombo(Y2OtherEdgeCombo,AllEdgePairs,COUNTOF(AllEdgePairs));
  180.  
  181.   FillCombo(X1UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  182.   FillCombo(X2UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  183.   FillCombo(Y1UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  184.   FillCombo(Y2UnitsCombo, UnitsPairs, COUNTOF(UnitsPairs));
  185.   
  186.   X1ValueEdit->SetText("");
  187.   X2ValueEdit->SetText("");
  188.   Y1ValueEdit->SetText("");
  189.   Y2ValueEdit->SetText("");
  190.  
  191.   FillCombo(X1RelationCombo, XRelationPairs, COUNTOF(XRelationPairs));
  192.   FillCombo(X2RelationCombo, XRelationPairs, COUNTOF(XRelationPairs));
  193.   FillCombo(Y1RelationCombo, YRelationPairs, COUNTOF(YRelationPairs));
  194.   FillCombo(Y2RelationCombo, YRelationPairs, COUNTOF(YRelationPairs));
  195.  
  196.   FillCombo(X1RelWinCombo, RelWinPairs, i+1);
  197.   FillCombo(X2RelWinCombo, RelWinPairs, i+1);
  198.   FillCombo(Y1RelWinCombo, RelWinPairs, i+1);
  199.   FillCombo(Y2RelWinCombo, RelWinPairs, i+1);
  200.  
  201.   // This call will result in a LBN_SELCHANGE Message being sent to the 
  202.   // window, which will result in HandleWindowChange being called.
  203.   // This call can't be made until the above controls are setup, so wait until
  204.   // the end here.
  205.   //
  206.   ChildNum = -1;  // Flag to signal initial call to HandleWindowChange
  207.   WindowList->SetSelIndex(0);
  208.   HandleWindowChange();
  209. }
  210.  
  211. static void SelectItemData(TComboBox* combo, uint32 itemData)
  212. {
  213.   int count = combo->GetCount();
  214.   combo->SetSelIndex(-1);  // Clear the selection
  215.   for (int i = 0; i < count; i++)
  216.     if (combo->GetItemData(i) == itemData) {
  217.       combo->SetSelIndex(i);
  218.       return;
  219.     }
  220.   TRACE("Couldn't find lmXxx (" << itemData <<") in list");
  221. }
  222.  
  223. static void SelectItemData(TComboBox* combo, TWindow* win)
  224. {
  225. #if defined(BI_MODEL_NEAR_DATA)
  226.   if (OFFSETOF(win) == 0)
  227.     SelectItemData(combo, uint32(0));
  228.   else
  229. #endif
  230.   SelectItemData(combo, uint32(win));
  231. }
  232.  
  233. //
  234. // Take a given layout constraint & shove it into the dialog
  235. //
  236. void TLayoutDialog::SetLayoutConstraint(TLayoutConstraint& lc, Constraint which)
  237. {
  238.   char buff[20];
  239.  
  240.   switch (which) {
  241.     case X1:
  242.       SelectItemData(X1EdgeCombo, lc.MyEdge);
  243.       SelectItemData(X1RelationCombo, lc.Relationship);
  244.       SelectItemData(X1RelWinCombo, lc.RelWin);
  245.       SelectItemData(X1OtherEdgeCombo, lc.OtherEdge);
  246.       SelectItemData(X1UnitsCombo, lc.Units);
  247.       wsprintf(buff, "%d", lc.Value);
  248.       X1ValueEdit->SetText(buff);
  249.       break;
  250.  
  251.     case X2:
  252.       SelectItemData(X2EdgeCombo, lc.MyEdge);
  253.       SelectItemData(X2RelationCombo, lc.Relationship);
  254.       SelectItemData(X2RelWinCombo, lc.RelWin);
  255.       SelectItemData(X2OtherEdgeCombo, lc.OtherEdge);
  256.       SelectItemData(X2UnitsCombo, lc.Units);
  257.       wsprintf(buff, "%d", lc.Value);
  258.       X2ValueEdit->SetText(buff);
  259.       break;
  260.  
  261.     case Y1:
  262.       SelectItemData(Y1EdgeCombo, lc.MyEdge);
  263.       SelectItemData(Y1RelationCombo, lc.Relationship);
  264.       SelectItemData(Y1RelWinCombo, lc.RelWin);
  265.       SelectItemData(Y1OtherEdgeCombo, lc.OtherEdge);
  266.       SelectItemData(Y1UnitsCombo, lc.Units);
  267.       wsprintf(buff, "%d", lc.Value);
  268.       Y1ValueEdit->SetText(buff);
  269.       break;
  270.  
  271.     case Y2:
  272.       SelectItemData(Y2EdgeCombo, lc.MyEdge);
  273.       SelectItemData(Y2RelationCombo, lc.Relationship);
  274.       SelectItemData(Y2RelWinCombo, lc.RelWin);
  275.       SelectItemData(Y2OtherEdgeCombo, lc.OtherEdge);
  276.       SelectItemData(Y2UnitsCombo, lc.Units);
  277.       wsprintf(buff, "%d", lc.Value);
  278.       Y2ValueEdit->SetText(buff);
  279.       break;
  280.   }
  281. }
  282.  
  283. //
  284. // Retrieve a layout constraint from the dialog
  285. //
  286. void TLayoutDialog::GetLayoutConstraint(TLayoutConstraint& lc, Constraint which)
  287. {
  288.   char buff[20];
  289.   switch (which) {
  290.     case X1:
  291.       lc.MyEdge = (TEdge)X1EdgeCombo->GetItemData(X1EdgeCombo->GetSelIndex());
  292.       lc.Relationship = (TRelationship)X1RelationCombo->GetItemData(X1RelationCombo->GetSelIndex());
  293.       lc.RelWin = (TWindow*)X1RelWinCombo->GetItemData(X1RelWinCombo->GetSelIndex());
  294.       lc.OtherEdge = (TEdge)X1OtherEdgeCombo->GetItemData(X1OtherEdgeCombo->GetSelIndex());
  295.       lc.Units = (TMeasurementUnits)X1UnitsCombo->GetItemData(X1UnitsCombo->GetSelIndex());
  296.       X1ValueEdit->GetText(buff, sizeof buff);
  297.       lc.Value = atoi(buff);
  298.       break;
  299.  
  300.     case X2:
  301.       lc.MyEdge = (TEdge)X2EdgeCombo->GetItemData(X2EdgeCombo->GetSelIndex());
  302.       lc.Relationship = (TRelationship)X2RelationCombo->GetItemData(X2RelationCombo->GetSelIndex());
  303.       lc.RelWin = (TWindow*)X2RelWinCombo->GetItemData(X2RelWinCombo->GetSelIndex());
  304.       lc.OtherEdge = (TEdge)X2OtherEdgeCombo->GetItemData(X2OtherEdgeCombo->GetSelIndex());
  305.       lc.Units = (TMeasurementUnits)X2UnitsCombo->GetItemData(X2UnitsCombo->GetSelIndex());
  306.       X2ValueEdit->GetText(buff, sizeof buff);
  307.       lc.Value = atoi(buff);
  308.       break;
  309.  
  310.     case Y1:
  311.       lc.MyEdge = (TEdge)Y1EdgeCombo->GetItemData(Y1EdgeCombo->GetSelIndex());
  312.       lc.Relationship = (TRelationship)Y1RelationCombo->GetItemData(Y1RelationCombo->GetSelIndex());
  313.       lc.RelWin = (TWindow*)Y1RelWinCombo->GetItemData(Y1RelWinCombo->GetSelIndex());
  314.       lc.OtherEdge = (TEdge)Y1OtherEdgeCombo->GetItemData(Y1OtherEdgeCombo->GetSelIndex());
  315.       lc.Units = (TMeasurementUnits)Y1UnitsCombo->GetItemData(Y1UnitsCombo->GetSelIndex());
  316.       Y1ValueEdit->GetText(buff, sizeof buff);
  317.       lc.Value = atoi(buff);
  318.       break;
  319.  
  320.     case Y2:
  321.       lc.MyEdge = (TEdge)Y2EdgeCombo->GetItemData(Y2EdgeCombo->GetSelIndex());
  322.       lc.Relationship = (TRelationship)Y2RelationCombo->GetItemData(Y2RelationCombo->GetSelIndex());
  323.       lc.RelWin = (TWindow*)Y2RelWinCombo->GetItemData(Y2RelWinCombo->GetSelIndex());
  324.       lc.OtherEdge = (TEdge)Y2OtherEdgeCombo->GetItemData(Y2OtherEdgeCombo->GetSelIndex());
  325.       lc.Units = (TMeasurementUnits)Y2UnitsCombo->GetItemData(Y2UnitsCombo->GetSelIndex());
  326.       Y2ValueEdit->GetText(buff, sizeof buff);
  327.       lc.Value = atoi(buff);
  328.       break;
  329.   }
  330. }
  331.  
  332. //
  333. // Window changed
  334. // Ask user to save current layout constraints?
  335. //
  336. void TLayoutDialog::HandleWindowChange()
  337. {
  338.   if (ChildNum >= 0) {
  339.     // Save all of the current settings, unless this is the first call
  340.     //
  341.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.X,X1);
  342.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Width,X2);
  343.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Y,Y1);
  344.     GetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Height,Y2);
  345.   }
  346.   // Switch to new window
  347.   //
  348.   ChildNum = WindowList->GetSelIndex();
  349.   if (ChildNum >= 0) {
  350.     SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.X,X1);
  351.     SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Width,X2);
  352.     SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Y,Y1);
  353.     SetLayoutConstraint(ChildInfo[ChildNum].LayoutMetrics.Height,Y2);
  354.   }
  355. }
  356.  
  357. //
  358. // Get the constraints from each window, update child window
  359. //
  360. void TLayoutDialog::HandleLayout()
  361. {
  362.   TLayoutMetrics& lm = ChildInfo[ChildNum].LayoutMetrics;
  363.  
  364.   GetLayoutConstraint(lm.X,X1);
  365.   GetLayoutConstraint(lm.Width,X2);
  366.   GetLayoutConstraint(lm.Y,Y1);
  367.   GetLayoutConstraint(lm.Height,Y2);
  368.  
  369.   // Check some basic restrictions
  370.   //
  371.   if (lm.X.MyEdge == lm.Width.MyEdge) {
  372.     MessageBox("X1.MyEdge can not be the same as X2.MyEdge");
  373.     return;
  374.   }
  375.   if (lm.Y.MyEdge == lm.Height.MyEdge) {
  376.     MessageBox("Y1.MyEdge can not be the same as Y2.MyEdge");
  377.     return;
  378.   }
  379.  
  380.   Parent->PostMessage(WM_COMMAND, CM_RELAYOUT);
  381. }
  382.