home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / ROLLDIAL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  3.6 KB  |  169 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. // Implementation of class TRollDialog, an automatic roll-up dialog
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_ROLLDIAL_H)
  11. # include <owl/rolldial.h>
  12. #endif
  13. #if !defined(OWL_MENU_H)
  14. # include <owl/menu.h>
  15. #endif
  16.  
  17. OWL_DIAGINFO;
  18.  
  19. //
  20. // Fibonacci sequence used for accelerated animation.
  21. //
  22. static uint32 Fibonacci(int n)
  23. {
  24.   if (n == 0 || n == 1) {
  25.     return 1;
  26.   }
  27.   uint32 older  = 1;
  28.   uint32 old    = 1;
  29.   uint32 answer = older + old;
  30.  
  31.   for (int i = 2; i < n; i++) {
  32.     older  = old;
  33.     old    = answer;
  34.     answer = old + older;
  35.   }
  36.   return answer;
  37. }
  38.  
  39.  
  40. DEFINE_RESPONSE_TABLE1(TRollDialog, TDialog)
  41.   EV_WM_SYSCOMMAND,
  42. END_RESPONSE_TABLE;
  43.  
  44. //
  45. // Setup data members for the various properties of the dialog.
  46. //
  47. TRollDialog::TRollDialog(TWindow* parent, TResId resId, bool animated,
  48.                          bool fullSize, TModule* module)
  49. :
  50.   InitialFullSize(fullSize),
  51.   IsFullSize(true),
  52.   WantAnimation(animated),
  53.   TDialog(parent, resId, module)
  54. {
  55. }
  56.  
  57. //
  58. // Adds the shrink system menu option is desired.
  59. // This also shrinks the dialog if that option was chosen.
  60. //
  61. void
  62. TRollDialog::SetupWindow()
  63. {
  64.   TDialog::SetupWindow();
  65.  
  66.   // Add to system menu if present
  67.   //
  68.   if (GetStyle() & WS_SYSMENU) {
  69.     TSystemMenu menu(*this);
  70.     menu.AppendMenu(MF_STRING, SC_ROLLDIALOG, "&Shrink");
  71.     HasSystemMenu = true;
  72.   }
  73.  
  74.   if (!InitialFullSize)
  75.     Shrink();
  76. }
  77.  
  78. //
  79. // Resize the dialog to either full size or minimal size.
  80. //
  81. void
  82. TRollDialog::Resize(bool currentlyFullSize)
  83. {
  84.   TRect r = GetWindowRect();
  85.   int oldBottom = r.bottom;
  86.  
  87.   if (WantAnimation) {
  88.     int delta = 0;
  89.     for (int step = 0; delta < Height; step++) {
  90.       if (currentlyFullSize)
  91.         r.bottom = oldBottom - delta;
  92.       else
  93.         r.bottom = oldBottom + delta;
  94.       MoveWindow(r, true);     
  95.       delta = (int)Fibonacci(step);
  96.     }
  97.   }
  98.  
  99.   if (currentlyFullSize)
  100.     r.bottom = oldBottom - Height;
  101.   else
  102.     r.bottom = oldBottom + Height;
  103.  
  104.   MoveWindow(r, true);
  105. }
  106.  
  107. //
  108. // Event handler for the system menu option "shrink".
  109. // Toggles the system menu choice to "expand".
  110. //
  111. void
  112. TRollDialog::Shrink()
  113. {
  114.   if (IsFullSize) {
  115.     Height = GetClientRect().Height() + 1;
  116.  
  117.     Resize(IsFullSize);
  118.     IsFullSize = false;
  119.  
  120.     if (HasSystemMenu) {
  121.       TSystemMenu menu(*this);
  122.       menu.ModifyMenu(SC_ROLLDIALOG, MF_BYCOMMAND | MF_STRING, SC_ROLLDIALOG,
  123.           "&Expand");
  124.     }
  125.   }
  126. }
  127.  
  128. //
  129. // Event handler for the system menu option "expand".
  130. // Toggles the system menu choice to "shrink".
  131. //
  132. void
  133. TRollDialog::Expand()
  134. {
  135.   if (!IsFullSize) {
  136.     Resize(IsFullSize);
  137.     IsFullSize = true;
  138.  
  139.     if (HasSystemMenu) {
  140.       TSystemMenu menu(*this);
  141.       menu.ModifyMenu(SC_ROLLDIALOG, MF_BYCOMMAND | MF_STRING, SC_ROLLDIALOG,
  142.           "&Shrink");
  143.     }
  144.   }
  145. }
  146.  
  147. //
  148. // Event handler for the system menu choice.
  149. // Calls either TRollDialog::Shrink or TRollDialog::Expand.
  150. //
  151. void
  152. TRollDialog::EvSysCommand(uint cmdType, TPoint& point)
  153. {
  154.   uint command = cmdType & 0xFFF0;
  155.  
  156.   if (command == SC_ROLLDIALOG || command == SC_RESTORE) {
  157.     if (IsFullSize)
  158.       Shrink();
  159.     else
  160.       Expand();
  161.   }
  162.   else if (command == SC_MINIMIZE)
  163.     Shrink();
  164.   else if (command == SC_MAXIMIZE)
  165.     Expand();
  166.   else
  167.     TDialog::EvSysCommand(cmdType, point);
  168. }
  169.