CGlobalOptions.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2002 Chris Schoeneman
00004  * 
00005  * This package is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "CConfig.h"
00016 #include "ProtocolTypes.h"
00017 #include "CStringUtil.h"
00018 #include "CArch.h"
00019 #include "CGlobalOptions.h"
00020 #include "LaunchUtil.h"
00021 #include "resource.h"
00022 
00023 static const int    s_defaultDelay     = 250;
00024 static const int    s_defaultHeartbeat = 5000;
00025 
00026 //
00027 // CGlobalOptions
00028 //
00029 
00030 CGlobalOptions*     CGlobalOptions::s_singleton = NULL;
00031 
00032 CGlobalOptions::CGlobalOptions(HWND parent, CConfig* config) :
00033     m_parent(parent),
00034     m_config(config),
00035     m_delayTime(s_defaultDelay),
00036     m_twoTapTime(s_defaultDelay),
00037     m_heartbeatTime(s_defaultHeartbeat)
00038 {
00039     assert(s_singleton == NULL);
00040     s_singleton = this;
00041 }
00042 
00043 CGlobalOptions::~CGlobalOptions()
00044 {
00045     s_singleton = NULL;
00046 }
00047 
00048 void
00049 CGlobalOptions::doModal()
00050 {
00051     // do dialog
00052     DialogBoxParam(s_instance, MAKEINTRESOURCE(IDD_GLOBAL_OPTIONS),
00053                                 m_parent, (DLGPROC)dlgProc, (LPARAM)this);
00054 }
00055 
00056 void
00057 CGlobalOptions::init(HWND hwnd)
00058 {
00059     HWND child;
00060     char buffer[30];
00061 
00062     // reset options
00063     sprintf(buffer, "%d", m_delayTime);
00064     child = getItem(hwnd, IDC_GLOBAL_DELAY_CHECK);
00065     setItemChecked(child, false);
00066     child = getItem(hwnd, IDC_GLOBAL_DELAY_TIME);
00067     setWindowText(child, buffer);
00068     sprintf(buffer, "%d", m_twoTapTime);
00069     child = getItem(hwnd, IDC_GLOBAL_TWO_TAP_CHECK);
00070     setItemChecked(child, false);
00071     child = getItem(hwnd, IDC_GLOBAL_TWO_TAP_TIME);
00072     setWindowText(child, buffer);
00073     sprintf(buffer, "%d", m_heartbeatTime);
00074     child = getItem(hwnd, IDC_GLOBAL_HEARTBEAT_CHECK);
00075     setItemChecked(child, false);
00076     child = getItem(hwnd, IDC_GLOBAL_HEARTBEAT_TIME);
00077     setWindowText(child, buffer);
00078     child = getItem(hwnd, IDC_GLOBAL_SCREENSAVER_SYNC);
00079     setItemChecked(child, true);
00080     child = getItem(hwnd, IDC_GLOBAL_RELATIVE_MOVES);
00081     setItemChecked(child, false);
00082     child = getItem(hwnd, IDC_GLOBAL_LEAVE_FOREGROUND);
00083     setItemChecked(child, false);
00084 
00085     // get the global options
00086     const CConfig::CScreenOptions* options = m_config->getOptions("");
00087     if (options != NULL) {
00088         for (CConfig::CScreenOptions::const_iterator index = options->begin();
00089                                         index != options->end(); ++index) {
00090             const OptionID id       = index->first;
00091             const OptionValue value = index->second;
00092             if (id == kOptionScreenSwitchDelay) {
00093                 if (value > 0) {
00094                     sprintf(buffer, "%d", value);
00095                     child = getItem(hwnd, IDC_GLOBAL_DELAY_CHECK);
00096                     setItemChecked(child, true);
00097                     child = getItem(hwnd, IDC_GLOBAL_DELAY_TIME);
00098                     setWindowText(child, buffer);
00099                 }
00100             }
00101             else if (id == kOptionScreenSwitchTwoTap) {
00102                 if (value > 0) {
00103                     sprintf(buffer, "%d", value);
00104                     child = getItem(hwnd, IDC_GLOBAL_TWO_TAP_CHECK);
00105                     setItemChecked(child, true);
00106                     child = getItem(hwnd, IDC_GLOBAL_TWO_TAP_TIME);
00107                     setWindowText(child, buffer);
00108                 }
00109             }
00110             else if (id == kOptionHeartbeat) {
00111                 if (value > 0) {
00112                     sprintf(buffer, "%d", value);
00113                     child = getItem(hwnd, IDC_GLOBAL_HEARTBEAT_CHECK);
00114                     setItemChecked(child, true);
00115                     child = getItem(hwnd, IDC_GLOBAL_HEARTBEAT_TIME);
00116                     setWindowText(child, buffer);
00117                 }
00118             }
00119             else if (id == kOptionScreenSaverSync) {
00120                 child = getItem(hwnd, IDC_GLOBAL_SCREENSAVER_SYNC);
00121                 setItemChecked(child, (value != 0));
00122             }
00123             else if (id == kOptionRelativeMouseMoves) {
00124                 child = getItem(hwnd, IDC_GLOBAL_RELATIVE_MOVES);
00125                 setItemChecked(child, (value != 0));
00126             }
00127             else if (id == kOptionWin32KeepForeground) {
00128                 child = getItem(hwnd, IDC_GLOBAL_LEAVE_FOREGROUND);
00129                 setItemChecked(child, (value != 0));
00130             }
00131         }
00132     }
00133 }
00134 
00135 bool
00136 CGlobalOptions::save(HWND hwnd)
00137 {
00138     HWND child;
00139     int newDelayTime     = 0;
00140     int newTwoTapTime    = 0;
00141     int newHeartbeatTime = 0;
00142 
00143     // get requested options
00144     child = getItem(hwnd, IDC_GLOBAL_DELAY_CHECK);
00145     if (isItemChecked(child)) {
00146         child         = getItem(hwnd, IDC_GLOBAL_DELAY_TIME);
00147         newDelayTime  = getTime(hwnd, child, true);
00148         if (newDelayTime == 0) {
00149             return false;
00150         }
00151     }
00152     else {
00153         child         = getItem(hwnd, IDC_GLOBAL_DELAY_TIME);
00154         newDelayTime  = getTime(hwnd, child, false);
00155         if (newDelayTime == 0) {
00156             newDelayTime = s_defaultDelay;
00157         }
00158     }
00159     child = getItem(hwnd, IDC_GLOBAL_TWO_TAP_CHECK);
00160     if (isItemChecked(child)) {
00161         child         = getItem(hwnd, IDC_GLOBAL_TWO_TAP_TIME);
00162         newTwoTapTime = getTime(hwnd, child, true);
00163         if (newTwoTapTime == 0) {
00164             return false;
00165         }
00166     }
00167     else {
00168         child         = getItem(hwnd, IDC_GLOBAL_TWO_TAP_TIME);
00169         newTwoTapTime = getTime(hwnd, child, false);
00170         if (newTwoTapTime == 0) {
00171             newTwoTapTime = s_defaultDelay;
00172         }
00173     }
00174     child = getItem(hwnd, IDC_GLOBAL_HEARTBEAT_CHECK);
00175     if (isItemChecked(child)) {
00176         child            = getItem(hwnd, IDC_GLOBAL_HEARTBEAT_TIME);
00177         newHeartbeatTime = getTime(hwnd, child, true);
00178         if (newHeartbeatTime == 0) {
00179             return false;
00180         }
00181     }
00182     else {
00183         child            = getItem(hwnd, IDC_GLOBAL_HEARTBEAT_TIME);
00184         newHeartbeatTime = getTime(hwnd, child, false);
00185         if (newHeartbeatTime == 0) {
00186             newHeartbeatTime = s_defaultHeartbeat;
00187         }
00188     }
00189 
00190     // remove existing config options
00191     m_config->removeOption("", kOptionScreenSwitchDelay);
00192     m_config->removeOption("", kOptionScreenSwitchTwoTap);
00193     m_config->removeOption("", kOptionHeartbeat);
00194     m_config->removeOption("", kOptionScreenSaverSync);
00195     m_config->removeOption("", kOptionRelativeMouseMoves);
00196     m_config->removeOption("", kOptionWin32KeepForeground);
00197 
00198     // add requested options
00199     child = getItem(hwnd, IDC_GLOBAL_DELAY_CHECK);
00200     if (isItemChecked(child)) {
00201         m_config->addOption("", kOptionScreenSwitchDelay, newDelayTime);
00202     }
00203     child = getItem(hwnd, IDC_GLOBAL_TWO_TAP_CHECK);
00204     if (isItemChecked(child)) {
00205         m_config->addOption("", kOptionScreenSwitchTwoTap, newTwoTapTime);
00206     }
00207     child = getItem(hwnd, IDC_GLOBAL_HEARTBEAT_CHECK);
00208     if (isItemChecked(child)) {
00209         m_config->addOption("", kOptionHeartbeat, newHeartbeatTime);
00210     }
00211     child = getItem(hwnd, IDC_GLOBAL_SCREENSAVER_SYNC);
00212     if (!isItemChecked(child)) {
00213         m_config->addOption("", kOptionScreenSaverSync, 0);
00214     }
00215     child = getItem(hwnd, IDC_GLOBAL_RELATIVE_MOVES);
00216     if (isItemChecked(child)) {
00217         m_config->addOption("", kOptionRelativeMouseMoves, 1);
00218     }
00219     child = getItem(hwnd, IDC_GLOBAL_LEAVE_FOREGROUND);
00220     if (isItemChecked(child)) {
00221         m_config->addOption("", kOptionWin32KeepForeground, 1);
00222     }
00223 
00224     // save last values
00225     m_delayTime     = newDelayTime;
00226     m_twoTapTime    = newTwoTapTime;
00227     m_heartbeatTime = newHeartbeatTime;
00228     return true;
00229 }
00230 
00231 int
00232 CGlobalOptions::getTime(HWND hwnd, HWND child, bool reportError)
00233 {
00234     CString valueString = getWindowText(child);
00235     int value = atoi(valueString.c_str());
00236     if (value < 1) {
00237         if (reportError) {
00238             showError(hwnd, CStringUtil::format(
00239                                 getString(IDS_INVALID_TIME).c_str(),
00240                                 valueString.c_str()));
00241             SetFocus(child);
00242         }
00243         return 0;
00244     }
00245     return value;
00246 }
00247 
00248 BOOL
00249 CGlobalOptions::doDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM)
00250 {
00251     switch (message) {
00252     case WM_INITDIALOG:
00253         init(hwnd);
00254         return TRUE;
00255 
00256     case WM_COMMAND:
00257         switch (LOWORD(wParam)) {
00258         case IDOK:
00259             if (save(hwnd)) {
00260                 EndDialog(hwnd, 0);
00261             }
00262             return TRUE;
00263 
00264         case IDCANCEL:
00265             EndDialog(hwnd, 0);
00266             return TRUE;
00267         }
00268         break;
00269 
00270     default:
00271         break;
00272     }
00273 
00274     return FALSE;
00275 }
00276 
00277 BOOL CALLBACK
00278 CGlobalOptions::dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
00279 {
00280     return s_singleton->doDlgProc(hwnd, message, wParam, lParam);
00281 }

Generated on Fri Nov 6 00:18:44 2009 for synergy-plus by  doxygen 1.4.7