The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2005 The Android Open Source Project |
| 3 | // |
| 4 | // Log preferences modal dialog. |
| 5 | // |
| 6 | |
| 7 | // For compilers that support precompilation, include "wx/wx.h". |
| 8 | #include "wx/wxprec.h" |
| 9 | // Otherwise, include all standard headers |
| 10 | #ifndef WX_PRECOMP |
| 11 | # include "wx/wx.h" |
| 12 | #endif |
| 13 | |
| 14 | #include "LogPrefsDialog.h" |
| 15 | #include "Preferences.h" |
| 16 | #include "Resource.h" |
| 17 | #include "utils.h" |
| 18 | |
| 19 | BEGIN_EVENT_TABLE(LogPrefsDialog, wxDialog) |
| 20 | EVT_CHECKBOX(IDC_LOG_PREFS_WRITE_FILE, LogPrefsDialog::OnWriteFile) |
| 21 | END_EVENT_TABLE() |
| 22 | |
| 23 | static const wxString gSpacerChoices[] = { |
| 24 | wxT("0"), wxT("1"), wxT("2") |
| 25 | }; |
| 26 | static const wxString gPointSizes[] = { |
| 27 | wxT("4"), wxT("6"), wxT("8"), wxT("10"), wxT("12"), wxT("14"), wxT("16") |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | * Constructor. |
| 33 | */ |
| 34 | LogPrefsDialog::LogPrefsDialog(wxWindow* parent) |
| 35 | : wxDialog(parent, IDD_LOG_PREFS, wxT("Log Preferences"), wxDefaultPosition, |
| 36 | wxDefaultSize, wxDEFAULT_DIALOG_STYLE), |
| 37 | mHeaderFormat(kHFFull), mSingleLine(false), mExtraSpacing(0), |
| 38 | mUseColor(false), mFontMonospace(false), mDisplayMax(0), mPoolSizeKB(0) |
| 39 | { |
| 40 | CreateControls(); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /* |
| 45 | * Destructor. Not much to do. |
| 46 | */ |
| 47 | LogPrefsDialog::~LogPrefsDialog(void) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Create all of the pages and add them to the notebook. |
| 53 | */ |
| 54 | void LogPrefsDialog::CreateControls(void) |
| 55 | { |
| 56 | wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL); |
| 57 | wxBoxSizer* okCancelSizer = new wxBoxSizer(wxHORIZONTAL); |
| 58 | mNotebook.Create(this, wxID_ANY); |
| 59 | wxPanel* page; |
| 60 | |
| 61 | page = CreateFormatPage(&mNotebook); |
| 62 | mNotebook.AddPage(page, wxT("Format"), true); |
| 63 | page = CreateLimitsPage(&mNotebook); |
| 64 | mNotebook.AddPage(page, wxT("Limits"), false); |
| 65 | page = CreateFilesPage(&mNotebook); |
| 66 | mNotebook.AddPage(page, wxT("Files"), false); |
| 67 | |
| 68 | // note to self: could use CreateButtonSizer here? |
| 69 | wxButton* cancel = new wxButton(this, wxID_CANCEL, wxT("&Cancel"), |
| 70 | wxDefaultPosition, wxDefaultSize, 0); |
| 71 | okCancelSizer->Add(cancel, 0, wxALL, kInterSpacing); |
| 72 | |
| 73 | wxButton* ok = new wxButton(this, wxID_OK, wxT("&OK"), |
| 74 | wxDefaultPosition, wxDefaultSize, 0); |
| 75 | okCancelSizer->Add(ok, 0, wxALL, kInterSpacing); |
| 76 | |
| 77 | mainSizer->Add(&mNotebook); |
| 78 | mainSizer->Add(okCancelSizer, 0, wxALIGN_RIGHT); |
| 79 | |
| 80 | SetSizer(mainSizer); |
| 81 | |
| 82 | mainSizer->Fit(this); // shrink-to-fit |
| 83 | mainSizer->SetSizeHints(this); // define minimum size |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Transfer data from our members to the window controls. |
| 88 | */ |
| 89 | bool LogPrefsDialog::TransferDataToWindow(void) |
| 90 | { |
| 91 | /* |
| 92 | * Do standard dialog setup. |
| 93 | */ |
| 94 | wxRadioButton* fmtFull = (wxRadioButton*) FindWindow(IDC_LOG_PREFS_FMT_FULL); |
| 95 | wxRadioButton* fmtBrief = (wxRadioButton*) FindWindow(IDC_LOG_PREFS_FMT_BRIEF); |
| 96 | wxRadioButton* fmtMinimal = (wxRadioButton*) FindWindow(IDC_LOG_PREFS_FMT_MINIMAL); |
| 97 | wxCheckBox* singleLine = (wxCheckBox*) FindWindow(IDC_LOG_PREFS_SINGLE_LINE); |
| 98 | wxComboBox* extraSpacing = (wxComboBox*) FindWindow(IDC_LOG_PREFS_EXTRA_SPACING); |
| 99 | wxComboBox* pointSize = (wxComboBox*) FindWindow(IDC_LOG_PREFS_POINT_SIZE); |
| 100 | wxCheckBox* useColor = (wxCheckBox*) FindWindow(IDC_LOG_PREFS_USE_COLOR); |
| 101 | wxCheckBox* fontMono = (wxCheckBox*) FindWindow(IDC_LOG_PREFS_FONT_MONO); |
| 102 | // - |
| 103 | wxTextCtrl* displayMax = (wxTextCtrl*) FindWindow(IDC_LOG_PREFS_DISPLAY_MAX); |
| 104 | wxTextCtrl* poolSize = (wxTextCtrl*) FindWindow(IDC_LOG_PREFS_POOL_SIZE); |
| 105 | // - |
| 106 | wxCheckBox* writeFile = (wxCheckBox*) FindWindow(IDC_LOG_PREFS_WRITE_FILE); |
| 107 | wxTextCtrl* fileName = (wxTextCtrl*) FindWindow(IDC_LOG_PREFS_FILENAME); |
| 108 | wxCheckBox* truncateOld = (wxCheckBox*) FindWindow(IDC_LOG_PREFS_TRUNCATE_OLD); |
| 109 | // - |
| 110 | |
| 111 | fmtFull->SetValue(mHeaderFormat == kHFFull); |
| 112 | fmtBrief->SetValue(mHeaderFormat == kHFBrief); |
| 113 | fmtMinimal->SetValue(mHeaderFormat == kHFMinimal); |
| 114 | singleLine->SetValue(mSingleLine); |
| 115 | if (mExtraSpacing < 0 || mExtraSpacing > NELEM(gSpacerChoices)) |
| 116 | mExtraSpacing = 0; |
| 117 | extraSpacing->SetSelection(mExtraSpacing); |
| 118 | |
| 119 | pointSize->SetSelection(0); |
| 120 | for (int i = 0; i < NELEM(gPointSizes); i++) { |
| 121 | if (atoi(gPointSizes[i].ToAscii()) == mPointSize) { |
| 122 | pointSize->SetSelection(i); |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | useColor->SetValue(mUseColor); |
| 127 | fontMono->SetValue(mFontMonospace); |
| 128 | |
| 129 | wxString tmpStr; |
| 130 | tmpStr.Printf(wxT("%d"), mDisplayMax); |
| 131 | displayMax->SetValue(tmpStr); |
| 132 | tmpStr.Printf(wxT("%d"), mPoolSizeKB); |
| 133 | poolSize->SetValue(tmpStr); |
| 134 | |
| 135 | writeFile->SetValue(mWriteFile); |
| 136 | fileName->SetValue(mFileName); |
| 137 | truncateOld->SetValue(mTruncateOld); |
| 138 | |
| 139 | EnableFileControls(mWriteFile); |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Convert a string to a number. The number is expected to be unsigned. |
| 146 | * Returns < 0 on failure. |
| 147 | */ |
| 148 | static long ConvertUnsigned(const wxString& str) |
| 149 | { |
| 150 | long val; |
| 151 | if (!str.ToLong(&val)) |
| 152 | return -1; |
| 153 | return val; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Transfer and validate data from the window controls. |
| 158 | * |
| 159 | * This doesn't get called if the user cancels out of the dialog. |
| 160 | */ |
| 161 | bool LogPrefsDialog::TransferDataFromWindow(void) |
| 162 | { |
| 163 | /* |
| 164 | * Do standard dialog export. |
| 165 | */ |
| 166 | //wxRadioButton* fmtFull = (wxRadioButton*) FindWindow(IDC_LOG_PREFS_FMT_FULL); |
| 167 | wxRadioButton* fmtBrief = (wxRadioButton*) FindWindow(IDC_LOG_PREFS_FMT_BRIEF); |
| 168 | wxRadioButton* fmtMinimal = (wxRadioButton*) FindWindow(IDC_LOG_PREFS_FMT_MINIMAL); |
| 169 | wxCheckBox* singleLine = (wxCheckBox*) FindWindow(IDC_LOG_PREFS_SINGLE_LINE); |
| 170 | wxComboBox* extraSpacing = (wxComboBox*) FindWindow(IDC_LOG_PREFS_EXTRA_SPACING); |
| 171 | wxComboBox* pointSize = (wxComboBox*) FindWindow(IDC_LOG_PREFS_POINT_SIZE); |
| 172 | wxCheckBox* useColor = (wxCheckBox*) FindWindow(IDC_LOG_PREFS_USE_COLOR); |
| 173 | wxCheckBox* fontMono = (wxCheckBox*) FindWindow(IDC_LOG_PREFS_FONT_MONO); |
| 174 | // - |
| 175 | wxTextCtrl* displayMax = (wxTextCtrl*) FindWindow(IDC_LOG_PREFS_DISPLAY_MAX); |
| 176 | wxTextCtrl* poolSize = (wxTextCtrl*) FindWindow(IDC_LOG_PREFS_POOL_SIZE); |
| 177 | // - |
| 178 | wxCheckBox* writeFile = (wxCheckBox*) FindWindow(IDC_LOG_PREFS_WRITE_FILE); |
| 179 | wxTextCtrl* fileName = (wxTextCtrl*) FindWindow(IDC_LOG_PREFS_FILENAME); |
| 180 | wxCheckBox* truncateOld = (wxCheckBox*) FindWindow(IDC_LOG_PREFS_TRUNCATE_OLD); |
| 181 | // - |
| 182 | |
| 183 | mHeaderFormat = kHFFull; |
| 184 | if (fmtBrief->GetValue()) |
| 185 | mHeaderFormat = kHFBrief; |
| 186 | else if (fmtMinimal->GetValue()) |
| 187 | mHeaderFormat = kHFMinimal; |
| 188 | |
| 189 | wxString tmpStr; |
| 190 | |
| 191 | mSingleLine = (singleLine->GetValue() != 0); |
| 192 | mExtraSpacing = extraSpacing->GetSelection(); |
| 193 | mPointSize = ConvertUnsigned(pointSize->GetValue()); |
| 194 | mUseColor = useColor->GetValue(); |
| 195 | mFontMonospace = fontMono->GetValue(); |
| 196 | |
| 197 | tmpStr = displayMax->GetValue(); |
| 198 | mDisplayMax = ConvertUnsigned(tmpStr); |
| 199 | if (mDisplayMax <= 0 || mDisplayMax > 1000 * 1000) { |
| 200 | wxMessageBox(wxT("Bad value for display max -- must be > 0 and <= 1,000,000"), |
| 201 | wxT("Hoser"), wxOK, this); |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | tmpStr = poolSize->GetValue(); |
| 206 | mPoolSizeKB = ConvertUnsigned(tmpStr); |
| 207 | if (mDisplayMax <= 0 || mDisplayMax > 1048576) { |
| 208 | wxMessageBox(wxT("Bad value for pool size -- must be > 0 and <= 1048576"), |
| 209 | wxT("Hoser"), wxOK, this); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | mWriteFile = (writeFile->GetValue() != 0); |
| 214 | mFileName = fileName->GetValue(); |
| 215 | mTruncateOld = (truncateOld->GetValue() != 0); |
| 216 | if (mWriteFile && mFileName.IsEmpty()) { |
| 217 | wxMessageBox(wxT("Log filename may not be blank"), |
| 218 | wxT("Hoser"), wxOK, this); |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /* |
| 227 | * Create the log Format page. |
| 228 | */ |
| 229 | wxPanel* LogPrefsDialog::CreateFormatPage(wxBookCtrlBase* parent) |
| 230 | { |
| 231 | wxPanel* panel = new wxPanel(parent); |
| 232 | |
| 233 | wxStaticBoxSizer* headerOpts = new wxStaticBoxSizer(wxVERTICAL, panel, |
| 234 | wxT("Header")); |
| 235 | headerOpts->Add(new wxRadioButton(panel, IDC_LOG_PREFS_FMT_FULL, |
| 236 | wxT("Full header"), wxDefaultPosition, wxDefaultSize, |
| 237 | wxRB_GROUP)); |
| 238 | headerOpts->Add(new wxRadioButton(panel, IDC_LOG_PREFS_FMT_BRIEF, |
| 239 | wxT("Brief header"))); |
| 240 | headerOpts->Add(new wxRadioButton(panel, IDC_LOG_PREFS_FMT_MINIMAL, |
| 241 | wxT("Minimal, integrated header"))); |
| 242 | |
| 243 | wxCheckBox* singleLine = new wxCheckBox(panel, IDC_LOG_PREFS_SINGLE_LINE, |
| 244 | wxT("Put headers and message on same line")); |
| 245 | |
| 246 | wxStaticText* extraSpacingDescr = new wxStaticText(panel, wxID_STATIC, |
| 247 | wxT("Extra line spacing:")); |
| 248 | wxComboBox* extraSpacing = new wxComboBox(panel, |
| 249 | IDC_LOG_PREFS_EXTRA_SPACING, wxT("blah"), |
| 250 | wxDefaultPosition, wxDefaultSize, NELEM(gSpacerChoices), |
| 251 | gSpacerChoices, wxCB_READONLY); |
| 252 | wxBoxSizer* extraSpacingSizer = new wxBoxSizer(wxHORIZONTAL); |
| 253 | extraSpacingSizer->Add(extraSpacingDescr, 0, wxALIGN_CENTER_VERTICAL); |
| 254 | extraSpacingSizer->AddSpacer(kInterSpacing); |
| 255 | extraSpacingSizer->Add(extraSpacing); |
| 256 | |
| 257 | wxStaticBoxSizer* textOpts = new wxStaticBoxSizer(wxVERTICAL, panel, |
| 258 | wxT("Text")); |
| 259 | textOpts->Add( |
| 260 | new wxStaticText(panel, wxID_STATIC, wxT("Point size:")) ); |
| 261 | textOpts->AddSpacer(kInterSpacing); |
| 262 | textOpts->Add( |
| 263 | new wxComboBox(panel, |
| 264 | IDC_LOG_PREFS_POINT_SIZE, wxT("blah"), |
| 265 | wxDefaultPosition, wxDefaultSize, NELEM(gPointSizes), |
| 266 | gPointSizes, wxCB_READONLY) ); |
| 267 | textOpts->AddSpacer(kInterSpacing); |
| 268 | textOpts->Add( |
| 269 | new wxCheckBox(panel, IDC_LOG_PREFS_USE_COLOR, |
| 270 | wxT("Colorful messages")) ); |
| 271 | textOpts->AddSpacer(kInterSpacing); |
| 272 | textOpts->Add( |
| 273 | new wxCheckBox(panel, IDC_LOG_PREFS_FONT_MONO, |
| 274 | wxT("Use monospace font")) ); |
| 275 | |
| 276 | |
| 277 | wxBoxSizer* sizerPanel = new wxBoxSizer(wxVERTICAL); |
| 278 | sizerPanel->Add(kMinWidth, kEdgeSpacing); // forces minimum width |
| 279 | sizerPanel->Add(headerOpts); |
| 280 | sizerPanel->AddSpacer(kInterSpacing); |
| 281 | sizerPanel->Add(singleLine); |
| 282 | sizerPanel->AddSpacer(kInterSpacing); |
| 283 | sizerPanel->Add(extraSpacingSizer); |
| 284 | sizerPanel->AddSpacer(kInterSpacing); |
| 285 | sizerPanel->Add(textOpts); |
| 286 | sizerPanel->AddSpacer(kInterSpacing); |
| 287 | |
| 288 | wxBoxSizer* horizIndent = new wxBoxSizer(wxHORIZONTAL); |
| 289 | horizIndent->AddSpacer(kEdgeSpacing); |
| 290 | horizIndent->Add(sizerPanel); |
| 291 | horizIndent->AddSpacer(kEdgeSpacing); |
| 292 | panel->SetSizer(horizIndent); |
| 293 | |
| 294 | return panel; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Create the log Limits page. |
| 299 | */ |
| 300 | wxPanel* LogPrefsDialog::CreateLimitsPage(wxBookCtrlBase* parent) |
| 301 | { |
| 302 | wxPanel* panel = new wxPanel(parent); |
| 303 | |
| 304 | wxBoxSizer* displayMaxSizer = new wxBoxSizer(wxHORIZONTAL); |
| 305 | displayMaxSizer->Add( |
| 306 | new wxStaticText(panel, wxID_ANY, |
| 307 | wxT("Maximum entries in log window:"), |
| 308 | wxDefaultPosition, wxDefaultSize, |
| 309 | wxALIGN_LEFT), |
| 310 | 0, wxALIGN_CENTER_VERTICAL); |
| 311 | displayMaxSizer->AddSpacer(kInterSpacing); |
| 312 | displayMaxSizer->Add( |
| 313 | new wxTextCtrl(panel, IDC_LOG_PREFS_DISPLAY_MAX)); |
| 314 | |
| 315 | wxBoxSizer* poolSizeSizer = new wxBoxSizer(wxHORIZONTAL); |
| 316 | poolSizeSizer->Add( |
| 317 | new wxStaticText(panel, wxID_ANY, |
| 318 | wxT("Size of the log pool (KB):"), |
| 319 | wxDefaultPosition, wxDefaultSize, |
| 320 | wxALIGN_LEFT), |
| 321 | 0, wxALIGN_CENTER_VERTICAL); |
| 322 | poolSizeSizer->AddSpacer(kInterSpacing); |
| 323 | poolSizeSizer->Add( |
| 324 | new wxTextCtrl(panel, IDC_LOG_PREFS_POOL_SIZE)); |
| 325 | |
| 326 | |
| 327 | wxBoxSizer* sizerPanel = new wxBoxSizer(wxVERTICAL); |
| 328 | sizerPanel->Add(kMinWidth, kEdgeSpacing); // forces minimum width |
| 329 | sizerPanel->Add(displayMaxSizer); |
| 330 | sizerPanel->AddSpacer(kInterSpacing); |
| 331 | sizerPanel->Add(poolSizeSizer); |
| 332 | sizerPanel->AddSpacer(kInterSpacing); |
| 333 | |
| 334 | wxBoxSizer* horizIndent = new wxBoxSizer(wxHORIZONTAL); |
| 335 | horizIndent->AddSpacer(kEdgeSpacing); |
| 336 | horizIndent->Add(sizerPanel); |
| 337 | horizIndent->AddSpacer(kEdgeSpacing); |
| 338 | panel->SetSizer(horizIndent); |
| 339 | |
| 340 | return panel; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Create the log Files page. |
| 345 | */ |
| 346 | wxPanel* LogPrefsDialog::CreateFilesPage(wxBookCtrlBase* parent) |
| 347 | { |
| 348 | wxPanel* panel = new wxPanel(parent); |
| 349 | wxStaticBoxSizer* logOpts = new wxStaticBoxSizer(wxVERTICAL, panel, |
| 350 | wxT("Log File")); |
| 351 | |
| 352 | wxCheckBox* writeCopy = |
| 353 | new wxCheckBox(panel, IDC_LOG_PREFS_WRITE_FILE, |
| 354 | wxT("Write a copy of log output to a file")); |
| 355 | |
| 356 | logOpts->AddSpacer(kInterSpacing); |
| 357 | logOpts->Add( |
| 358 | new wxStaticText(panel, wxID_ANY, |
| 359 | wxT("Filename:"), |
| 360 | wxDefaultPosition, wxDefaultSize, |
| 361 | wxALIGN_LEFT)); |
| 362 | logOpts->AddSpacer(kInterSpacing); |
| 363 | logOpts->Add( |
| 364 | new wxTextCtrl(panel, IDC_LOG_PREFS_FILENAME), 0, wxEXPAND); |
| 365 | logOpts->AddSpacer(kInterSpacing); |
| 366 | logOpts->Add( |
| 367 | new wxCheckBox(panel, IDC_LOG_PREFS_TRUNCATE_OLD, |
| 368 | wxT("Truncate the file if more than 8 hours old ")) ); |
| 369 | |
| 370 | |
| 371 | wxBoxSizer* sizerPanel = new wxBoxSizer(wxVERTICAL); |
| 372 | sizerPanel->Add(kMinWidth, kEdgeSpacing); // forces minimum width |
| 373 | sizerPanel->Add(writeCopy); |
| 374 | sizerPanel->AddSpacer(kInterSpacing); |
| 375 | sizerPanel->Add(logOpts); |
| 376 | sizerPanel->AddSpacer(kInterSpacing); |
| 377 | |
| 378 | wxBoxSizer* horizIndent = new wxBoxSizer(wxHORIZONTAL); |
| 379 | horizIndent->AddSpacer(kEdgeSpacing); |
| 380 | horizIndent->Add(sizerPanel); |
| 381 | horizIndent->AddSpacer(kEdgeSpacing); |
| 382 | panel->SetSizer(horizIndent); |
| 383 | |
| 384 | return panel; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /* |
| 389 | * Handle clicks on the "write file" checkbox. |
| 390 | */ |
| 391 | void LogPrefsDialog::OnWriteFile(wxCommandEvent& event) |
| 392 | { |
| 393 | EnableFileControls(event.GetInt()); |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Enable or disable some of the controls on the "file" page. |
| 398 | */ |
| 399 | void LogPrefsDialog::EnableFileControls(bool enable) |
| 400 | { |
| 401 | FindWindow(IDC_LOG_PREFS_FILENAME)->Enable(enable); |
| 402 | FindWindow(IDC_LOG_PREFS_TRUNCATE_OLD)->Enable(enable); |
| 403 | } |
| 404 | |