PrefCommand.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is Vision.
  13. *
  14. * The Initial Developer of the Original Code is The Vision Team.
  15. * Portions created by The Vision Team are
  16. * Copyright (C) 1999, 2000, 2001 The Vision Team. All Rights
  17. * Reserved.
  18. *
  19. * Contributor(s): Rene Gollent
  20. * Todd Lair
  21. */
  22. #include "PrefCommand.h"
  23. #include "Vision.h"
  24. #include "VTextControl.h"
  25. #include <ScrollView.h>
  26. static const char *CommandControlLabels[] =
  27. {
  28. S_PREFCOMMAND_QUIT,
  29. S_PREFCOMMAND_KICK,
  30. S_PREFCOMMAND_IGNORE,
  31. S_PREFCOMMAND_UNIGNORE,
  32. S_PREFCOMMAND_AWAY,
  33. S_PREFCOMMAND_BACK,
  34. S_PREFCOMMAND_UPTIME,
  35. 0
  36. };
  37. CommandPrefsView::CommandPrefsView (BRect frame)
  38. : BView (frame, "Command prefs", B_FOLLOW_ALL_SIDES, B_WILL_DRAW | B_FRAME_EVENTS)
  39. {
  40. SetViewColor (ui_color (B_PANEL_BACKGROUND_COLOR));
  41. BRect bounds (Bounds());
  42. int32 i (0);
  43. bounds.left += 3;
  44. bounds.right -= B_V_SCROLL_BAR_WIDTH + 3;
  45. bounds.top += 3;
  46. bounds.bottom -= 5;
  47. float label_width (0.0);
  48. for (i = 0; CommandControlLabels[i]; ++i)
  49. if (StringWidth (CommandControlLabels[i]) > label_width)
  50. label_width = StringWidth (CommandControlLabels[i]);
  51. BView *bgView (new BView (bounds, "", B_FOLLOW_ALL_SIDES, B_WILL_DRAW));
  52. bgView->SetViewColor (ui_color (B_PANEL_BACKGROUND_COLOR));
  53. fCommands = new VTextControl * [MAX_COMMANDS];
  54. for (i = 0; i < MAX_COMMANDS; ++i)
  55. {
  56. fCommands[i] = new VTextControl (
  57. BRect (5, be_plain_font->Size() + ((1.5 * i) * 1.5 * be_plain_font->Size()), 5 + bounds.right - be_plain_font->StringWidth("gP"),
  58. be_plain_font->Size() + (1.5 * (i + 1) * 1.5 * be_plain_font->Size())),
  59. "commands",
  60. CommandControlLabels[i],
  61. vision_app->GetCommand (i).String(),
  62. NULL,
  63. B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP);
  64. fCommands[i]->SetDivider (label_width + 5);
  65. BMessage *msg (new BMessage (M_COMMAND_MODIFIED));
  66. msg->AddInt32 ("which", i);
  67. fCommands[i]->SetModificationMessage (msg);
  68. bgView->AddChild (fCommands[i]);
  69. }
  70. fScroller = new BScrollView("command scroller", bgView, B_FOLLOW_ALL_SIDES,
  71. 0, false, true);
  72. BScrollBar *bar (fScroller->ScrollBar(B_VERTICAL));
  73. fMaxheight = bgView->Bounds().Height();
  74. fProportionheight = fCommands[MAX_COMMANDS-1]->Frame().bottom + 10.0;
  75. bar->SetRange (0.0, (fProportionheight - fScroller->Bounds().Height()));
  76. bar->SetProportion (fScroller->Bounds().Height() / fProportionheight);
  77. AddChild (fScroller);
  78. }
  79. CommandPrefsView::~CommandPrefsView (void)
  80. {
  81. delete [] fCommands;
  82. }
  83. void
  84. CommandPrefsView::AttachedToWindow (void)
  85. {
  86. BView::AttachedToWindow ();
  87. for (int32 i = 0; i < MAX_COMMANDS; i++)
  88. fCommands[i]->SetTarget (this);
  89. }
  90. void
  91. CommandPrefsView::AllAttached (void)
  92. {
  93. BView::AllAttached ();
  94. }
  95. void
  96. CommandPrefsView::FrameResized (float width, float height)
  97. {
  98. BView::FrameResized (width, height);
  99. BScrollBar *bar(fScroller->ScrollBar(B_VERTICAL));
  100. if (!bar)
  101. return;
  102. float min, max, scrollheight (fScroller->Bounds().Height());
  103. bar->GetRange (&min, &max);
  104. if (scrollheight < fProportionheight)
  105. {
  106. if (max != fMaxheight)
  107. bar->SetRange (0.0, fProportionheight - scrollheight);
  108. bar->SetProportion (scrollheight / fProportionheight);
  109. }
  110. else
  111. {
  112. bar->SetProportion (1.0);
  113. bar->SetRange (0.0, 0.0);
  114. }
  115. }
  116. void
  117. CommandPrefsView::MessageReceived (BMessage *msg)
  118. {
  119. switch (msg->what)
  120. {
  121. case M_COMMAND_MODIFIED:
  122. {
  123. int32 which;
  124. msg->FindInt32 ("which", &which);
  125. vision_app->SetCommand (
  126. which,
  127. fCommands[which]->TextView()->Text());
  128. }
  129. break;
  130. default:
  131. BView::MessageReceived (msg);
  132. }
  133. }