Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.4 KiB

  1. // Dlg class.
  2. //
  3. // Provides some helper methods to resize a dialog window.
  4. function Dlg()
  5. {
  6. this._cxPerEm = 1;
  7. this._cyPerEm = 1;
  8. this._dxFrame = 0;
  9. this._dyFrame = 0;
  10. }
  11. /*-------------------------------------------------------------------------
  12. Purpose: Calculates pixels-per-em metrics, based upon the given dialog size string
  13. ("dialogWidth:xxx; dialogHeight:yyy").
  14. The body of the dialog requires a DIV with width and height set to 100%.
  15. This is needed so we can account for the frame of the dialog when we
  16. resize (client vs. window rect).
  17. */
  18. function Dlg_CalcMetrics(szDlgSize, idDivDlg)
  19. {
  20. // There's a race condition where offsetHeight can change in the middle of this
  21. // function if the content is greater than the window. So take a snapshot of it
  22. // now.
  23. var cxDiv = idDivDlg.offsetWidth;
  24. var cyDiv = idDivDlg.offsetHeight;
  25. var ichWidth = szDlgSize.indexOf("dialogWidth:") + 12; // 12 = cch of "dialogWidth:"
  26. var ichHeight = szDlgSize.indexOf("dialogHeight:") + 13; // 13 = cch of "dialogHeight:"
  27. var cxDlgEm = parseInt(szDlgSize.substring(ichWidth));
  28. var cyDlgEm = parseInt(szDlgSize.substring(ichHeight));
  29. /*
  30. alert('dialogWidth=' + window.dialogWidth + '; offsetWidth=' + cxDiv +
  31. '; clientWidth=' + idDivDlg.clientWidth + '\n' +
  32. 'dialogHeight=' + window.dialogHeight + '; offsetHeight=' + cyDiv +
  33. '; clientHeight=' + idDivDlg.clientHeight + '\n' +
  34. 'cxDlgEm=' + cxDlgEm + '; cyDlgEm=' + cyDlgEm);
  35. */
  36. // dialogWidth is in pixels when read, but is in ems when written.
  37. // The inconsistency amazes me...but I'll make use of that fact!
  38. var cxWindow = parseInt(window.dialogWidth); // we expect this to be in pixels
  39. var cyWindow = parseInt(window.dialogHeight); // we expect this to be in pixels
  40. // We need to set the window size of the dialog, which is bigger than the client
  41. // size. So calculate the difference so we can adjust the rectangle appropriately.
  42. this._dxFrame = cxWindow - cxDiv;
  43. this._dyFrame = cyWindow - cyDiv;
  44. this._cxPerEm = cxWindow / cxDlgEm;
  45. this._cyPerEm = cyWindow / cyDlgEm;
  46. }
  47. function Dlg_CxToEms(cx)
  48. {
  49. return cx / this._cxPerEm;
  50. }
  51. function Dlg_CyToEms(cy)
  52. {
  53. return cy / this._cyPerEm;
  54. }
  55. /*-------------------------------------------------------------------------
  56. Purpose: Resize the dialog to the given width and height (in pixels).
  57. */
  58. function Dlg_Resize(cx, cy)
  59. {
  60. var cxNew = this.CxToEms(cx + this._dxFrame);
  61. var cyNew = this.CyToEms(cy + this._dyFrame);
  62. // Set the dialog size to entirely accomodate the contents of the dialog
  63. /*
  64. alert('cx=' + cx + '; cxNew=' + cxNew + '; this._dxFrame=' + this._dxFrame + '\n' +
  65. 'cy=' + cy + '; cyNew=' + cyNew + '; this._dyFrame=' + this._dyFrame);
  66. */
  67. // We cannot simply use the ResizeTo or ResizeBy methods because they don't
  68. // work for dialog windows.
  69. window.dialogWidth = cxNew;
  70. window.dialogHeight = cyNew;
  71. }
  72. /*-------------------------------------------------------------------------
  73. Purpose: Initialize the Dlg class
  74. */
  75. function InitDlgClass()
  76. {
  77. // Create and discard an initial Dlg object for prototypes
  78. new Dlg();
  79. // Dlg Methods
  80. Dlg.prototype.CalcMetrics = Dlg_CalcMetrics;
  81. Dlg.prototype.Resize = Dlg_Resize;
  82. Dlg.prototype.CxToEms = Dlg_CxToEms;
  83. Dlg.prototype.CyToEms = Dlg_CyToEms;
  84. }