home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / APPLAUNC.PAK / APPBTNBA.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  5KB  |  206 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //----------------------------------------------------------------------------
  4. #include <owl\owlpch.h>
  5. #include <owl\applicat.h>
  6. #include <math.h>
  7. #include "appbtnba.h"
  8. #include "applaunc.rh"
  9.  
  10. DEFINE_RESPONSE_TABLE1(TAppButtonBar, TToolBox)
  11.   EV_WM_RBUTTONDOWN,
  12.   EV_WM_LBUTTONUP,
  13.   EV_WM_LBUTTONDOWN,
  14.   EV_WM_MOUSEMOVE,
  15. END_RESPONSE_TABLE;
  16.  
  17. const int TAppButtonBar::AppButtonIdBase = 500;
  18.  
  19.  
  20. void TAppButtonBar::SetupWindow()
  21. {
  22.   TToolBox::SetupWindow();
  23.   DragState = 0;
  24.   OrigCursor = 0;
  25.   DragCursor = ::LoadCursor(GetModule()->GetInstance(),
  26.                             MAKEINTRESOURCE(IDC_DRAG_BUTTON));
  27.   CM_PROPERTIES = ::RegisterWindowMessage("CM_PROPERTIES");
  28.   CM_BUTTON_PRESSED = ::RegisterWindowMessage("CM_BUTTON_PRESSED");
  29.   CM_BUTTON_DRAG = ::RegisterWindowMessage("CM_BUTTON_DRAG");
  30.  
  31. }
  32.  
  33. //
  34. // MoveButton(). Move a button, specified by 'srcLoc', to another location,
  35. // specified by 'destLoc'.  The button to be 'moved' is removed from the
  36. // button bar and then a new one (with same characteristics) is re-inserted into
  37. // the proper place.  Assumes that all the buttons will be re-ided after call.
  38. //
  39. void
  40. TAppButtonBar::MoveButton(int srcLoc, int destLoc, const string& iconPath )
  41. {
  42.   TAppButton* toMove = ButtonWithId(IdFromLoc(srcLoc));
  43.   TAppButton* toInsertBefore = ButtonWithId(IdFromLoc(destLoc));
  44.   TAppButton* newButton = new TAppButton(GetApplication()->GetInstance(),
  45.                                          iconPath, IdFromLoc(srcLoc));
  46.  
  47.   if (toInsertBefore)
  48.     Insert(*newButton, Before, toInsertBefore);
  49.   else
  50.     Insert(*newButton, Before, GadgetWithId(IDB_APPREMOVER));
  51.   delete Remove(*toMove);
  52. }
  53.  
  54. //
  55. // ChangeOrientation(). Change layout of application buttons (vertical or
  56. // horizontal).
  57. //
  58. void
  59. TAppButtonBar::ChangeOrientation(TTileDirection direction)
  60. {
  61.   SetDirection(direction);
  62. }
  63.  
  64. //
  65. // DestroyButton(). Delete a button given by location.
  66. //
  67. void
  68. TAppButtonBar::DestroyButton(int loc)
  69. {
  70.   TAppButton* b = ButtonWithId(IdFromLoc(loc));
  71.   Remove(*b);
  72.   delete b;
  73. }
  74.  
  75. //
  76. // Return button with given 'real' id.
  77. //
  78. TAppButton*
  79. TAppButtonBar::ButtonWithId(int id)
  80. {
  81.   TAppButton* b = TYPESAFE_DOWNCAST(FirstGadget(), TAppButton);
  82.   while (b) {
  83.     if (b->RealId == id)
  84.       return b;
  85.     else
  86.       b = TYPESAFE_DOWNCAST(NextGadget(*b), TAppButton);
  87.   }
  88.   return 0;
  89. }
  90.  
  91. //
  92. // Flush().  Remove all buttons from button bar.
  93. //
  94. void
  95. TAppButtonBar::Flush(int del)
  96. {
  97.   TGadget* g = FirstGadget();
  98.   while (g) {
  99.     Remove(*g);
  100.     if (del)
  101.       delete g;
  102.     g = FirstGadget();
  103.   }
  104. }
  105.  
  106. //
  107. // EvRButtonDown(). Send message (CM_PROPERTIES) to main window so
  108. // properties dialog can be displayed for this button.
  109. //
  110. void
  111. TAppButtonBar::EvRButtonDown(UINT /*modKeys*/, TPoint& point)
  112. {
  113.   TAppButton* temp;
  114.   if ((temp = TYPESAFE_DOWNCAST(GadgetFromPoint(point), TAppButton)) != 0)
  115.     Parent->PostMessage(CM_PROPERTIES, temp->RealId, 0);
  116. }
  117.  
  118. //
  119. // EvLButtonUp(). If a drag was in program send a message, CM_BUTTON_DRAG,
  120. // to main window.  If button was simple pressed send message,
  121. // CM_BUTTON_PRESSED, to main window so selected app can be run.
  122. //
  123. void
  124. TAppButtonBar::EvLButtonUp(UINT modKeys, TPoint& point)
  125. {
  126.   TAppButton* temp = TYPESAFE_DOWNCAST(GadgetFromPoint(point), TAppButton);
  127.  
  128.   if (DragState == 2) {
  129.     TPoint  screenPoint(point);
  130.  
  131.     ClientToScreen(screenPoint);
  132.     ResetOrigCursor();
  133.     if ((temp && temp->RealId != DragButtonId) || !temp &&
  134.         Parent->GetWindowRect().Contains(screenPoint))
  135.       Parent->PostMessage(CM_BUTTON_DRAG, DragButtonId,
  136.                           MAKELPARAM(point.x, point.y));
  137.   } else {
  138.     if (temp != 0)
  139.       Parent->PostMessage(CM_BUTTON_PRESSED, temp->RealId, LPARAM(HWindow));
  140.   }
  141.   DragState = 0;
  142.   TToolBox::EvLButtonUp(modKeys, point);
  143. }
  144.  
  145. //
  146. // EvLButtonDown(). Indicate that a possible button drag is in progress.
  147. //
  148. void
  149. TAppButtonBar::EvLButtonDown(UINT modKeys, TPoint& point)
  150. {
  151.   TAppButton* temp = TYPESAFE_DOWNCAST(GadgetFromPoint(point), TAppButton);
  152.   if (temp && temp->GetId() != IDB_APPREMOVER) {
  153.     DragButtonId = temp->RealId;
  154.     DragState = 1;
  155.     StartDragPoint = point;
  156.   }
  157.   TToolBox::EvLButtonDown(modKeys, point);
  158. }
  159.  
  160. //
  161. // EvMouseMove().  If dragging, set the cursor.  A check is made
  162. // to see if we have moved far enough to regard the mouse move as a drag.
  163. //
  164. void
  165. TAppButtonBar::EvMouseMove(UINT modKeys, TPoint& point)
  166. {
  167.   if (DragState == 1 && (abs(point.x - StartDragPoint.x) > 5 ||
  168.                          abs(point.y - StartDragPoint.y) > 5)) {
  169.     DragState = 2;
  170.     SetDragCursor();
  171.   }
  172.   TToolBox::EvMouseMove(modKeys, point);
  173. }
  174.  
  175. //
  176. // ReDraw(). Begin a layout session. Then move window so everthing gets
  177. // drawn again.
  178. //
  179. void
  180. TAppButtonBar::ReDraw()
  181. {
  182.   LayoutSession();
  183. }
  184.  
  185. //
  186. // SetDragCursor().  Change cursor to button drag cursor.
  187. //
  188. void
  189. TAppButtonBar::SetDragCursor()
  190. {
  191.   if (!OrigCursor)
  192.     OrigCursor = ::GetCursor();
  193.   ::SetCursor(DragCursor);
  194. }
  195.  
  196. //
  197. // ResetOrigCursor().  Change cursor back to whatever is was before.
  198. //
  199. void
  200. TAppButtonBar::ResetOrigCursor()
  201. {
  202.   if (OrigCursor)
  203.     ::SetCursor(OrigCursor);
  204.   OrigCursor = 0;
  205. }
  206.