dlg.js
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 4k
Category:

Windows Kernel

Development Platform:

Visual C++

  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. */
  19. function Dlg_CalcMetrics(szDlgSize, idDivDlg)
  20. {
  21.     // There's a race condition where offsetHeight can change in the middle of this
  22.     // function if the content is greater than the window.  So take a snapshot of it
  23.     // now.
  24.     var cxDiv = idDivDlg.offsetWidth;
  25.     var cyDiv = idDivDlg.offsetHeight;
  26.     
  27.     var ichWidth = szDlgSize.indexOf("dialogWidth:") + 12;   // 12 = cch of "dialogWidth:"
  28.     var ichHeight = szDlgSize.indexOf("dialogHeight:") + 13; // 13 = cch of "dialogHeight:"
  29.     var cxDlgEm = parseInt(szDlgSize.substring(ichWidth));
  30.     var cyDlgEm = parseInt(szDlgSize.substring(ichHeight));
  31.     /*
  32.     alert('dialogWidth=' + window.dialogWidth + '; offsetWidth=' + cxDiv +
  33.           '; clientWidth=' + idDivDlg.clientWidth + 'n' +
  34.           'dialogHeight=' + window.dialogHeight + '; offsetHeight=' + cyDiv +
  35.           '; clientHeight=' + idDivDlg.clientHeight + 'n' +
  36.           'cxDlgEm=' + cxDlgEm + '; cyDlgEm=' + cyDlgEm);
  37.     */
  38.     
  39.     // dialogWidth is in pixels when read, but is in ems when written.
  40.     // The inconsistency amazes me...but I'll make use of that fact!
  41.     
  42.     var cxWindow = parseInt(window.dialogWidth);        // we expect this to be in pixels
  43.     var cyWindow = parseInt(window.dialogHeight);       // we expect this to be in pixels
  44.     
  45.     // We need to set the window size of the dialog, which is bigger than the client
  46.     // size.  So calculate the difference so we can adjust the rectangle appropriately.
  47.     
  48.     this._dxFrame = cxWindow - cxDiv;
  49.     this._dyFrame = cyWindow - cyDiv;
  50.     
  51.     this._cxPerEm = cxWindow / cxDlgEm;
  52.     this._cyPerEm = cyWindow / cyDlgEm;
  53. }
  54. function Dlg_CxToEms(cx)
  55. {
  56.     return cx / this._cxPerEm;
  57. }
  58. function Dlg_CyToEms(cy)
  59. {
  60.     return cy / this._cyPerEm;
  61. }
  62. /*-------------------------------------------------------------------------
  63. Purpose: Resize the dialog to the given width and height (in pixels).
  64. */
  65. function Dlg_Resize(cx, cy)
  66. {
  67.     var cxNew = this.CxToEms(cx + this._dxFrame);
  68.     var cyNew = this.CyToEms(cy + this._dyFrame);
  69.     
  70.     // Set the dialog size to entirely accomodate the contents of the dialog
  71.     /*
  72.     alert('cx=' + cx + '; cxNew=' + cxNew + '; this._dxFrame=' + this._dxFrame + 'n' +
  73.           'cy=' + cy + '; cyNew=' + cyNew + '; this._dyFrame=' + this._dyFrame);
  74.     */
  75.     
  76.     // We cannot simply use the ResizeTo or ResizeBy methods because they don't
  77.     // work for dialog windows.
  78.     
  79.     window.dialogWidth = cxNew;
  80.     window.dialogHeight = cyNew;
  81. }
  82. /*-------------------------------------------------------------------------
  83. Purpose: Initialize the Dlg class
  84. */
  85. function InitDlgClass()
  86. {
  87.     // Create and discard an initial Dlg object for prototypes
  88.     new Dlg();
  89.     // Dlg Methods
  90.     Dlg.prototype.CalcMetrics = Dlg_CalcMetrics;
  91.     Dlg.prototype.Resize = Dlg_Resize;
  92.     Dlg.prototype.CxToEms = Dlg_CxToEms;
  93.     Dlg.prototype.CyToEms = Dlg_CyToEms;
  94. }