ServerEntryWindow.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. */
  21. #include <Box.h>
  22. #include <Button.h>
  23. #include <CheckBox.h>
  24. #include <MenuField.h>
  25. #include <Menu.h>
  26. #include <MenuItem.h>
  27. #include <Window.h>
  28. #include "NumericFilter.h"
  29. #include "ServerEntryWindow.h"
  30. #include "Vision.h"
  31. #include "VTextControl.h"
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. ServerEntryWindow::ServerEntryWindow (BHandler *handler, BMessage *invoked, const ServerData *data, int32 size)
  35. : BWindow (
  36. BRect (50, 50, 350, 250),
  37. S_SERVERWIN_TITLE,
  38. B_TITLED_WINDOW,
  39. B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS)
  40. {
  41. AddChild (new ServerEntryView (Bounds(), handler, invoked, data, size));
  42. }
  43. ServerEntryWindow::~ServerEntryWindow (void)
  44. {
  45. }
  46. ServerEntryView::ServerEntryView (BRect bounds, BHandler *handler, BMessage *invoked, const ServerData *data, int32 size)
  47. : BView (
  48. bounds,
  49. "entryView",
  50. B_FOLLOW_ALL_SIDES,
  51. B_WILL_DRAW),
  52. invocation (invoked),
  53. target (handler)
  54. {
  55. ASSERT (handler != NULL);
  56. memset(&currentServer, 0, sizeof(ServerData));
  57. if (size != 0)
  58. memcpy(&currentServer, data, size);
  59. SetViewColor (ui_color (B_PANEL_BACKGROUND_COLOR));
  60. serverName = new VTextControl (BRect (0,0,0,0), "serverName", S_SERVERWIN_SERVER,
  61. (data) ? data->serverName : "", new BMessage (M_SERVER_NAME_CHANGED),
  62. B_FOLLOW_LEFT | B_FOLLOW_TOP);
  63. BString strPort ("");
  64. if (data) strPort << data->port;
  65. else strPort << 6667;
  66. port = new VTextControl (BRect (0,0,0,0), "portVal", S_SERVERWIN_PORT,
  67. strPort.String(), new BMessage (M_SERVER_PORT_CHANGED),
  68. B_FOLLOW_LEFT | B_FOLLOW_TOP);
  69. port->SetDivider (be_plain_font->StringWidth ("Port: ") + 5);
  70. BMenu *stateMenu = new BMenu (S_SERVERWIN_MENU1);
  71. stateMenu->AddItem (new BMenuItem (S_SERVERWIN_MENU_PRI, new BMessage (M_SERVER_STATE)));
  72. stateMenu->AddItem (new BMenuItem (S_SERVERWIN_MENU_SEC, new BMessage (M_SERVER_STATE)));
  73. stateMenu->AddItem (new BMenuItem (S_SERVERWIN_MENU_DIS , new BMessage (M_SERVER_STATE)));
  74. statusField = new BMenuField (BRect (0,0,0,0), "states", S_SERVERWIN_STATE, stateMenu,
  75. B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE);
  76. okButton = new BButton (BRect (0,0,0,0), "serverOk", S_SERVERWIN_DONE_BUTTON,
  77. new BMessage (M_SERVER_DONE), B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE);
  78. cancelButton = new BButton (BRect (0,0,0,0), "serverCancel", S_SERVERWIN_CANCEL_BUTTON,
  79. new BMessage (M_SERVER_CANCEL), B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE);
  80. BString password ("");
  81. if (strlen(currentServer.password) > 0)
  82. password = currentServer.password;
  83. usePassword = new BCheckBox (BRect (0,0,0,0), "usePass", S_SERVERWIN_PASS_CHECK,
  84. new BMessage (M_SERVER_USEPASS), B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE);
  85. passwordField = new VTextControl (BRect (0,0,0,0), "password", NULL,
  86. password.String(), NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE);
  87. AddChild (statusField);
  88. AddChild (serverName);
  89. AddChild (port);
  90. AddChild (okButton);
  91. AddChild (cancelButton);
  92. AddChild (usePassword);
  93. AddChild (passwordField);
  94. }
  95. ServerEntryView::~ServerEntryView (void)
  96. {
  97. delete invocation;
  98. }
  99. void
  100. ServerEntryView::AttachedToWindow (void)
  101. {
  102. BView::AttachedToWindow();
  103. serverName->SetDivider (be_plain_font->StringWidth (S_SERVERWIN_SERVER) + 5);
  104. serverName->ResizeToPreferred();
  105. serverName->ResizeTo (Bounds().Width() / 2, serverName->Bounds().Height());
  106. serverName->MoveTo (10,10);
  107. serverName->SetTarget (this);
  108. port->ResizeToPreferred();
  109. port->MoveTo (serverName->Frame().right + 15, serverName->Frame().top);
  110. port->SetTarget (this);
  111. port->TextView()->AddFilter (new NumericFilter());
  112. float diff (0);
  113. if ((diff = (port->Frame().right - Bounds().right)) > 0.0)
  114. ResizeBy (diff, 0.0);
  115. statusField->ResizeToPreferred();
  116. statusField->Menu()->SetTargetForItems (this);
  117. usePassword->ResizeToPreferred();
  118. usePassword->MoveTo (serverName->Frame().left, serverName->Frame().bottom + 15);
  119. usePassword->SetTarget(this);
  120. usePassword->SetValue ((strlen(currentServer.password) > 0) ? B_CONTROL_ON : B_CONTROL_OFF);
  121. passwordField->SetEnabled(usePassword->Value() == B_CONTROL_ON);
  122. passwordField->ResizeToPreferred();
  123. passwordField->ResizeTo(port->Frame().right - usePassword->Frame().right - 5, passwordField->Bounds().Height());
  124. passwordField->MoveTo (usePassword->Frame().right + 5, usePassword->Frame().top);
  125. passwordField->TextView()->HideTyping(true);
  126. #if B_BEOS_VERSION_DANO
  127. statusField->MoveTo ((Bounds().Width() - statusField->Bounds().Width()) / 2.0,
  128. #else
  129. statusField->MoveTo ((Bounds().Width() - statusField->Bounds().Width()) / 4.0,
  130. #endif
  131. usePassword->Frame().bottom + 15);
  132. cancelButton->SetTarget (this);
  133. okButton->SetTarget (this);
  134. okButton->ResizeToPreferred();
  135. okButton->MoveTo (port->Frame().right - okButton->Bounds().Width(), statusField->Frame().bottom + 30);
  136. cancelButton->ResizeToPreferred();
  137. cancelButton->MoveTo (okButton->Frame().left - (cancelButton->Frame().Width() + 5), okButton->Frame().top);
  138. okButton->SetEnabled (false);
  139. Window()->ResizeTo (okButton->Frame().right + 5, okButton->Frame().bottom + 10);
  140. dynamic_cast<BInvoker *>(statusField->Menu()->ItemAt(currentServer.state))->Invoke();
  141. }
  142. void
  143. ServerEntryView::AllAttached (void)
  144. {
  145. BView::AllAttached();
  146. port->MakeFocus (false);
  147. port->MakeFocus (true);
  148. serverName->MakeFocus(false);
  149. serverName->MakeFocus(true);
  150. }
  151. void
  152. ServerEntryView::CheckDoneState (void)
  153. {
  154. if (serverName->TextView()->TextLength() != 0)
  155. if (port->TextView()->TextLength() != 0)
  156. if (statusField->Menu()->FindMarked() != NULL)
  157. {
  158. okButton->SetEnabled (true);
  159. return;
  160. }
  161. okButton->SetEnabled (false);
  162. }
  163. void
  164. ServerEntryView::MessageReceived (BMessage *msg)
  165. {
  166. switch (msg->what)
  167. {
  168. case M_SERVER_STATE:
  169. {
  170. BMenu *menu (statusField->Menu());
  171. BMenuItem *item (NULL);
  172. item = menu->FindMarked();
  173. if (item)
  174. item->SetMarked (false);
  175. msg->FindPointer ("source", reinterpret_cast<void **>(&item));
  176. item->SetMarked (true);
  177. statusField->MenuItem()->SetLabel (item->Label());
  178. }
  179. case M_SERVER_NAME_CHANGED:
  180. case M_SERVER_PORT_CHANGED:
  181. CheckDoneState();
  182. break;
  183. case M_SERVER_USEPASS:
  184. {
  185. passwordField->SetEnabled (usePassword->Value() == B_CONTROL_ON);
  186. }
  187. break;
  188. case M_SERVER_DONE:
  189. {
  190. BMenu *menu (statusField->Menu());
  191. ServerData data;
  192. memset (&data, 0, sizeof (ServerData));
  193. strcpy (data.serverName, serverName->Text());
  194. data.port = atoi (port->Text());
  195. data.state = menu->IndexOf(menu->FindMarked());
  196. if (usePassword->Value() == B_CONTROL_ON)
  197. strcpy(data.password, passwordField->TextView()->Text());
  198. BMessenger msgr (target);
  199. BMessage msg (*invocation);
  200. msg.AddData ("server", B_RAW_TYPE, &data, sizeof(data));
  201. msgr.SendMessage (&msg);
  202. }
  203. case M_SERVER_CANCEL:
  204. Window()->PostMessage (B_QUIT_REQUESTED);
  205. break;
  206. default:
  207. BView::MessageReceived (msg);
  208. }
  209. }