Move device and mountpoint selection to configuration to eliminate the need of asking...
[Rockbox.git] / rbutil / rbutilCtrls.cpp
blobfdede634d321a3f2e263a93ea9deddf34201cdee
2 #include "rbutilCtrls.h"
3 #include "bootloaders.h"
4 #include "autodetection.h"
6 /////////////////////////////////////////////////////////////
7 //// Controls
8 ////////////////////////////////////////////////////////////////
10 /////////////////////////////////////////////
11 //// Image Ctrl
12 //////////////////////////////////////////////
14 BEGIN_EVENT_TABLE(ImageCtrl, wxControl)
15 EVT_PAINT(ImageCtrl::OnPaint)
16 END_EVENT_TABLE()
18 IMPLEMENT_DYNAMIC_CLASS(ImageCtrl, wxControl)
20 bool ImageCtrl::Create(wxWindow* parent, wxWindowID id,
21 const wxPoint& pos, const wxSize& size, long style,
22 const wxValidator& validator)
24 if (!wxControl::Create(parent, id, pos, size, style, validator)) return false;
26 return true;
29 void ImageCtrl::OnPaint(wxPaintEvent& event)
31 wxPaintDC dc(this);
32 dc.DrawBitmap(m_bitmap,0,0,false);
35 void ImageCtrl::SetBitmap(wxBitmap bmp)
37 m_bitmap = bmp;
38 Refresh();
42 wxSize ImageCtrl::DoGetBestSize() const
44 wxSize bestsize;
45 bestsize.x = m_bitmap.GetWidth();
46 bestsize.y = m_bitmap.GetHeight();
47 return bestsize;
52 /////////////////////////////////////////////
53 //// Theme Control
54 //////////////////////////////////////////////
56 BEGIN_EVENT_TABLE(ThemeCtrl, wxPanel)
57 EVT_LISTBOX(ID_THEME_LST, ThemeCtrl::OnThemesLst)
58 EVT_BUTTON(ID_THEME_SELECT_ALL, ThemeCtrl::OnSelectAll)
59 END_EVENT_TABLE()
61 IMPLEMENT_DYNAMIC_CLASS(ThemeCtrl, wxPanel)
63 bool ThemeCtrl::Create(wxWindow* parent, wxWindowID id,
64 const wxPoint& pos, const wxSize& size, long style,
65 const wxString title )
67 if (!wxPanel::Create(parent, id, pos, size, style, title)) return false;
69 CreateControls();
71 GetSizer()->Fit(this);
73 GetSizer()->SetSizeHints(this);
74 return true;
77 void ThemeCtrl::CreateControls()
79 // A top-level sizer
80 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
81 this->SetSizer(topSizer);
83 wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
84 topSizer->Add(horizontalSizer, 0, wxALIGN_LEFT|wxALL, 5);
86 //Device Selection
87 wxBoxSizer* wxBoxSizer7 = new wxBoxSizer(wxVERTICAL);
88 horizontalSizer->Add(wxBoxSizer7,0,wxGROW | wxALL,0);
90 wxStaticText* m_desc = new wxStaticText( this, wxID_STATIC,
91 wxT("Select one or more Themes to install"), wxDefaultPosition,
92 wxDefaultSize, 0 );
93 wxBoxSizer7->Add(m_desc, 0, wxALIGN_LEFT|wxALL, 5);
95 m_themeList = new wxListBox(this,ID_THEME_LST,wxDefaultPosition,
96 wxDefaultSize,0,NULL,wxLB_EXTENDED);
97 wxBoxSizer7->Add(m_themeList, 0, wxALIGN_LEFT|wxALL, 5);
99 m_selectAllThemes = new wxButton(this, ID_THEME_SELECT_ALL,
100 wxT("Select All"));
101 wxBoxSizer7->Add(m_selectAllThemes, 0, wxALIGN_LEFT|wxALL, 5);
103 // Preview Picture
104 wxBoxSizer* wxBoxSizer9 = new wxBoxSizer(wxVERTICAL);
105 horizontalSizer->Add(wxBoxSizer9,0,wxGROW | wxALL,0);
107 wxStaticText* preview_desc= new wxStaticText(this,wxID_ANY,wxT("Preview:"));
108 wxBoxSizer9->Add(preview_desc,0,wxGROW | wxALL,5);
110 m_PreviewBitmap = new ImageCtrl(this,ID_PREVIEW_BITMAP );
111 wxBoxSizer9->Add(m_PreviewBitmap,0,wxALIGN_LEFT | wxALL,5);
113 wxStaticBox* groupbox= new wxStaticBox(this,wxID_ANY,wxT("Selected Theme:"));
114 wxStaticBoxSizer* styleSizer = new wxStaticBoxSizer( groupbox, wxVERTICAL );
115 topSizer->Add(styleSizer,0,wxGROW|wxALL,0);
117 // horizontal sizer
118 wxBoxSizer* wxBoxSizer8 = new wxBoxSizer(wxHORIZONTAL);
119 styleSizer->Add(wxBoxSizer8,0,wxGROW | wxALL,0);
121 // File size
122 wxStaticText* size_desc= new wxStaticText(this,wxID_ANY,wxT("Filesize:"));
123 wxBoxSizer8->Add(size_desc,0,wxGROW | wxALL,5);
125 m_size= new wxStaticText(this,ID_FILESIZE,wxT(""));
126 wxBoxSizer8->Add(m_size,0,wxGROW | wxALL,5);
128 // Description
129 wxStaticText* desc_desc= new wxStaticText(this,wxID_ANY,wxT("Description:"));
130 styleSizer->Add(desc_desc,0,wxGROW | wxALL,5);
132 m_themedesc= new wxTextCtrl(this,ID_DESC,wxT(""),wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY);
133 styleSizer->Add(m_themedesc,0,wxGROW | wxALL,5);
135 topSizer->Fit(this);
136 topSizer->SetSizeHints(this);
137 Layout();
141 void ThemeCtrl::Init()
143 m_Themes.Clear();
144 m_Themes_path.Clear();
145 m_Themes_size.Clear();
146 m_Themes_image.Clear();
147 m_Themes_desc.Clear();
151 void ThemeCtrl::setDevice(wxString device)
154 int index = gv->plat_id.Index(device);
155 if(index == -1) return;
157 if(gv->plat_resolution[index] == m_currentResolution)
158 return;
159 else
160 m_currentResolution = gv->plat_resolution[index];
162 // load the themelist
163 Init();
164 m_size->SetLabel(wxT(""));
165 m_themedesc->SetValue(wxT(""));
166 m_themeList->Clear();
168 //get correct Themes list
169 wxString src,dest,err;
171 src = gv->themes_url + wxT("rbutil.php?res=") + m_currentResolution;
172 dest = gv->stdpaths->GetUserDataDir() + wxT("" PATH_SEP "download" PATH_SEP)
173 + m_currentResolution + wxT(".list");
175 if(DownloadURL(src, dest))
177 MESG_DIALOG(wxT("Unable to download themes list."));
178 return;
181 //read and parse Themes list
182 wxString themelistraw;
183 wxFFile themefile;
184 if(!themefile.Open(dest)) //open file
186 MESG_DIALOG(wxT("Unable to open themes list."));
187 return;
189 if(!themefile.ReadAll(&themelistraw)) //read complete file
191 MESG_DIALOG(wxT("Unable to read themes list."));
192 return;
194 wxRegEx reAll(wxT("<body >(.+)</body>")); //extract body part
195 if(! reAll.Matches(themelistraw))
197 MESG_DIALOG(wxT("Themes list is in wrong Format."));
198 return;
200 wxString lines = reAll.GetMatch(themelistraw,1);
202 // prepare text
203 lines.Replace(wxT("<br />"),wxT(""),true); //replace <br /> with nothing
204 lines.Replace(wxT("\n"),wxT(""),true); //replace \n with nothing
205 lines.Trim(true); //strip WS at end
206 lines.Trim(false); //strip WS at beginning
207 wxStringTokenizer tkz(lines,wxT("|")); //tokenize it
209 while ( tkz.HasMoreTokens() ) // read all entrys
211 m_Themes.Add(tkz.GetNextToken()); //Theme name
212 m_Themes_path.Add(tkz.GetNextToken()); //Theme path
213 m_Themes_size.Add(tkz.GetNextToken()); //File size
214 m_Themes_image.Add(tkz.GetNextToken()); //Screenshot
215 m_Themes_desc.Add(tkz.GetNextToken()); //Description
217 m_themeList->Append(m_Themes.Last());
220 this->GetSizer()->Layout();
221 this->GetSizer()->Fit(this);
222 this->GetSizer()->SetSizeHints(this);
223 m_parent->GetSizer()->Layout();
224 m_parent->GetSizer()->Fit(m_parent);
225 m_parent->GetSizer()->SetSizeHints(m_parent);
229 void ThemeCtrl::OnThemesLst(wxCommandEvent& event)
231 ThemePreview();
234 void ThemeCtrl::ThemePreview()
236 // wxCriticalSectionLocker locker(m_ThemeSelectSection);
238 wxArrayInt selected;
239 int numSelected = m_themeList->GetSelections(selected);
240 if(numSelected == 0) return;
242 int index = selected[0];
244 m_size->SetLabel(m_Themes_size[index]);
245 m_themedesc->SetValue(m_Themes_desc[index]);
246 // m_themedesc->Wrap(200); // wrap desc
248 wxString src,dest;
250 int pos = m_Themes_image[index].Find('/',true);
251 wxString filename = m_Themes_image[index](pos+1,m_Themes_image[index].Length());
253 dest = gv->stdpaths->GetUserDataDir()
254 + wxT("" PATH_SEP "download" PATH_SEP)
255 + m_currentResolution;
257 if(!wxDirExists(dest))
258 wxMkdir(dest);
260 //this is a URL no PATH_SEP
261 src = gv->themes_url + wxT("/data/") + m_currentResolution + wxT("/")
262 + filename;
263 dest = gv->stdpaths->GetUserDataDir() + wxT("" PATH_SEP "download" PATH_SEP)
264 + m_currentResolution + wxT("" PATH_SEP) + filename;
266 if(!wxFileExists(dest))
268 if(DownloadURL(src, dest))
270 MESG_DIALOG(wxT("Unable to download image."));
271 return;
275 m_currentimage = dest;
276 wxBitmap bmp;
277 bmp.LoadFile(m_currentimage,wxBITMAP_TYPE_PNG);
278 m_PreviewBitmap->SetBitmap(bmp);
280 Refresh();
281 this->GetSizer()->Layout();
282 this->GetSizer()->Fit(this);
283 this->GetSizer()->SetSizeHints(this);
285 m_parent->GetSizer()->Layout();
286 m_parent->GetSizer()->Fit(m_parent);
287 m_parent->GetSizer()->SetSizeHints(m_parent);
291 void ThemeCtrl::OnSelectAll(wxCommandEvent& event)
293 for(unsigned int i=0; i < m_themeList->GetCount(); i++)
294 m_themeList->Select(i);
295 ThemePreview();
298 wxArrayString ThemeCtrl::getThemesToInstall()
300 wxArrayString themes;
301 wxArrayInt selected;
302 int numSelected = m_themeList->GetSelections(selected);
304 for(int i=0; i < numSelected; i++)
306 themes.Add(m_Themes_path[selected[i]]);
308 return themes;
312 /////////////////////////////////////////////
313 //// Ok Cancel Control
314 //////////////////////////////////////////////
316 BEGIN_EVENT_TABLE(OkCancelCtrl, wxPanel)
318 END_EVENT_TABLE()
320 IMPLEMENT_DYNAMIC_CLASS(OkCancelCtrl, wxPanel)
322 bool OkCancelCtrl::Create(wxWindow* parent, wxWindowID id,
323 const wxPoint& pos, const wxSize& size, long style,
324 const wxString title)
326 if (!wxPanel::Create(parent, id, pos, size, style, title)) return false;
328 CreateControls();
329 GetSizer()->Fit(this);
330 GetSizer()->SetSizeHints(this);
331 return true;
334 void OkCancelCtrl::CreateControls()
336 // A top-level sizer
337 wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
338 this->SetSizer(topSizer);
340 // The OK button
341 m_OkBtn = new wxButton ( this, wxID_OK, wxT("&OK"),
342 wxDefaultPosition, wxDefaultSize, 0 );
343 topSizer->Add(m_OkBtn, 0, wxALL, 5);
344 // The Cancel button
345 m_CancelBtn = new wxButton ( this, wxID_CANCEL,
346 wxT("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
347 topSizer->Add(m_CancelBtn, 0, wxALL, 5);
349 Layout();
354 /////////////////////////////////////////////
355 //// Device Selector
356 //////////////////////////////////////////////
358 BEGIN_EVENT_TABLE(DeviceSelectorCtrl, wxPanel)
359 EVT_BUTTON(ID_AUTODETECT_BTN, DeviceSelectorCtrl::OnAutoDetect)
360 EVT_COMBOBOX(ID_DEVICE_CBX,DeviceSelectorCtrl::OnComboBox)
361 END_EVENT_TABLE()
363 IMPLEMENT_DYNAMIC_CLASS(DeviceSelectorCtrl, wxPanel)
365 bool DeviceSelectorCtrl::Create(wxWindow* parent, wxWindowID id,
366 const wxPoint& pos, const wxSize& size, long style,
367 const wxString title)
369 if (!wxPanel::Create(parent, id, pos, size, style, title)) return false;
371 CreateControls();
372 GetSizer()->Fit(this);
373 GetSizer()->SetSizeHints(this);
374 return true;
377 void DeviceSelectorCtrl::CreateControls()
379 // A top-level sizer
380 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
381 this->SetSizer(topSizer);
383 //Device Selection
384 wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
385 topSizer->Add(horizontalSizer, 0, wxALIGN_LEFT|wxALL, 5);
386 m_desc = new wxStaticText( this, wxID_STATIC,
387 wxT("Device:"), wxDefaultPosition,
388 wxDefaultSize, 0 );
389 horizontalSizer->Add(m_desc, 0, wxALIGN_LEFT|wxALL, 5);
391 m_deviceCbx = new wxComboBox(this, ID_DEVICE_CBX,wxT("Select your Device"),
392 wxDefaultPosition,wxDefaultSize,gv->plat_name,wxCB_READONLY);
394 m_deviceCbx->SetToolTip(wxT("Select your Device."));
395 m_deviceCbx->SetHelpText(wxT("Select your Device."));
397 horizontalSizer->Add(m_deviceCbx, 0, wxALIGN_LEFT|wxALL, 5);
399 wxButton* m_autodetectBtn = new wxButton(this, ID_AUTODETECT_BTN, wxT("Autodetect"),
400 wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
401 wxT("AutodetectBtn"));
403 m_autodetectBtn->SetToolTip(wxT("Click here to autodetect your Device."));
404 m_autodetectBtn->SetHelpText(wxT("Autodetection of the Device."));
405 // m_autodetectBtn->SetFocus();
407 horizontalSizer->Add(m_autodetectBtn,0,wxGROW | wxALL,5);
408 Layout();
412 wxString DeviceSelectorCtrl::getDevice()
414 return m_currentDevice;
417 void DeviceSelectorCtrl::setDefault()
419 int index = gv->plat_id.Index(gv->curplat);
420 if(index == -1) return;
421 m_deviceCbx->SetValue(gv->plat_name[index]);
424 void DeviceSelectorCtrl::OnComboBox(wxCommandEvent& event)
426 int index = gv->plat_name.Index(m_deviceCbx->GetValue());
428 if(index == -1)
430 m_currentDevice = wxT("");
431 return;
434 gv->curplat = gv->plat_id[index];
437 void DeviceSelectorCtrl::OnAutoDetect(wxCommandEvent& event)
439 if(!AutoDetect())
441 WARN_DIALOG(wxT("No Device detected. If you have a Device connected, select the correct one manually."),
442 wxT("Detecting a Device"));
447 bool DeviceSelectorCtrl::AutoDetect()
449 UsbDeviceInfo device;
451 if(detectDevices(&device))
454 if(device.status == DEVICEFOUND)
456 m_deviceCbx->SetValue(gv->plat_name[device.device_index]);
457 gv->curplat=gv->plat_id[device.device_index];
459 if(device.path != wxT(""))
461 gv->curdestdir = device.path;
463 return true;
465 else if(device.status == TOMANYDEVICES)
467 WARN_DIALOG(wxT("More then one device detected, please connect only One"),
468 wxT("Detecting a Device"));
469 return true;
472 return false;
475 /////////////////////////////////////////////
476 //// DevicePosition Selector
477 //////////////////////////////////////////////
479 BEGIN_EVENT_TABLE(DevicePositionCtrl, wxPanel)
480 EVT_BUTTON(ID_BROWSE_BTN, DevicePositionCtrl::OnBrowseBtn)
481 END_EVENT_TABLE()
483 IMPLEMENT_DYNAMIC_CLASS(DevicePositionCtrl, wxPanel)
485 bool DevicePositionCtrl::Create(wxWindow* parent, wxWindowID id,
486 const wxPoint& pos, const wxSize& size, long style,
487 const wxString title)
489 if (!wxPanel::Create(parent, id, pos, size, style, title)) return false;
491 CreateControls();
492 GetSizer()->Fit(this);
493 GetSizer()->SetSizeHints(this);
494 return true;
497 void DevicePositionCtrl::CreateControls()
499 // A top-level sizer
500 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
501 this->SetSizer(topSizer);
503 //Device Selection
504 m_desc = new wxStaticText( this, wxID_STATIC,
505 wxT("Select your Device in the Filesystem"), wxDefaultPosition,
506 wxDefaultSize, 0 );
507 topSizer->Add(m_desc, 0, wxALIGN_LEFT|wxALL, 5);
509 wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
510 topSizer->Add(horizontalSizer, 0, wxGROW|wxALL, 5);
512 m_devicePos = new wxTextCtrl(this,wxID_ANY,gv->curdestdir);
513 m_devicePos->SetToolTip(wxT("Type the folder where your Device is here"));
514 m_devicePos->SetHelpText(wxT("Type the folder where your Device is here"));
515 horizontalSizer->Add(m_devicePos,0,wxGROW | wxALL,5);
517 m_browseBtn = new wxButton(this, ID_BROWSE_BTN, wxT("Browse"),
518 wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
519 wxT("BrowseBtn"));
520 m_browseBtn->SetToolTip(wxT("Browse for your Device"));
521 m_browseBtn->SetHelpText(wxT("Browse for your Device"));
522 horizontalSizer->Add(m_browseBtn,0,wxGROW | wxALL,5);
524 topSizer->Fit(this);
525 Layout();
529 wxString DevicePositionCtrl::getDevicePos()
531 return m_devicePos->GetValue();
535 void DevicePositionCtrl::setDefault()
537 m_devicePos->SetValue(gv->curdestdir);
540 void DevicePositionCtrl::OnBrowseBtn(wxCommandEvent& event)
542 const wxString& temp = wxDirSelector(
543 wxT("Please select the location of your audio device"), gv->curdestdir,
544 0, wxDefaultPosition, this);
546 if (!temp.empty())
548 m_devicePos->SetValue(temp);
553 /////////////////////////////////////////////
554 //// FirmwarePosition Selector
555 //////////////////////////////////////////////
557 BEGIN_EVENT_TABLE(FirmwarePositionCtrl, wxPanel)
558 EVT_BUTTON(ID_BROWSE_BTN, FirmwarePositionCtrl::OnBrowseBtn)
559 END_EVENT_TABLE()
561 IMPLEMENT_DYNAMIC_CLASS(FirmwarePositionCtrl, wxControl)
563 bool FirmwarePositionCtrl::Create(wxWindow* parent, wxWindowID id,
564 const wxPoint& pos, const wxSize& size, long style,
565 const wxString title)
567 if (!wxPanel::Create(parent, id, pos, size, style, title)) return false;
569 CreateControls();
570 GetSizer()->Fit(this);
571 GetSizer()->SetSizeHints(this);
572 return true;
575 void FirmwarePositionCtrl::CreateControls()
577 // A top-level sizer
578 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
579 this->SetSizer(topSizer);
581 //Device Selection
582 m_desc = new wxStaticText( this, wxID_STATIC,
583 wxT("Select original Firmware from the Manufacturer"), wxDefaultPosition,
584 wxDefaultSize, 0 );
585 topSizer->Add(m_desc, 0, wxALIGN_LEFT|wxALL, 5);
587 wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
588 topSizer->Add(horizontalSizer, 0, wxALIGN_LEFT|wxALL, 5);
590 m_firmwarePos = new wxTextCtrl(this,wxID_ANY,gv->curdestdir);
591 m_firmwarePos->SetToolTip(wxT("Type the folder where the original Firmware is here"));
592 m_firmwarePos->SetHelpText(wxT("Type the folder where the original Firmware is here"));
593 horizontalSizer->Add(m_firmwarePos,0,wxGROW | wxALL,5);
595 m_browseBtn = new wxButton(this, ID_BROWSE_BTN, wxT("Browse"),
596 wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
597 wxT("BrowseBtn"));
598 m_browseBtn->SetToolTip(wxT("Browse for the original Firmware"));
599 m_browseBtn->SetHelpText(wxT("Browse for the original Firmware"));
600 horizontalSizer->Add(m_browseBtn,0,wxGROW | wxALL,5);
602 Layout();
606 wxString FirmwarePositionCtrl::getFirmwarePos()
608 return m_firmwarePos->GetValue();
612 void FirmwarePositionCtrl::setDefault()
614 m_firmwarePos->SetValue(gv->curfirmware);
617 void FirmwarePositionCtrl::OnBrowseBtn(wxCommandEvent& event)
619 wxString temp = wxFileSelector(
620 wxT("Please select the location of the original Firmware"), gv->curdestdir,wxT(""),wxT(""),wxT("*.hex"));
622 if (!temp.empty())
624 m_firmwarePos->SetValue(temp);