rbutil: Add quessing of the Drive letter via Windows API, also restructuring for...
[Rockbox.git] / rbutil / rbutilCtrls.cpp
blob887f2e7fb1b31f3d7eb808f3dcd3beed1f2db5fb
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(DownloadURL(src, dest))
268 MESG_DIALOG(wxT("Unable to download image."));
269 return;
272 m_currentimage = dest;
273 wxBitmap bmp;
274 bmp.LoadFile(m_currentimage,wxBITMAP_TYPE_PNG);
275 m_PreviewBitmap->SetBitmap(bmp);
277 Refresh();
278 this->GetSizer()->Layout();
279 this->GetSizer()->Fit(this);
280 this->GetSizer()->SetSizeHints(this);
282 m_parent->GetSizer()->Layout();
283 m_parent->GetSizer()->Fit(m_parent);
284 m_parent->GetSizer()->SetSizeHints(m_parent);
288 void ThemeCtrl::OnSelectAll(wxCommandEvent& event)
290 for(unsigned int i=0; i < m_themeList->GetCount(); i++)
291 m_themeList->Select(i);
292 ThemePreview();
295 wxArrayString ThemeCtrl::getThemesToInstall()
297 wxArrayString themes;
298 wxArrayInt selected;
299 int numSelected = m_themeList->GetSelections(selected);
301 for(int i=0; i < numSelected; i++)
303 themes.Add(m_Themes_path[selected[i]]);
305 return themes;
309 /////////////////////////////////////////////
310 //// Ok Cancel Control
311 //////////////////////////////////////////////
313 BEGIN_EVENT_TABLE(OkCancelCtrl, wxPanel)
315 END_EVENT_TABLE()
317 IMPLEMENT_DYNAMIC_CLASS(OkCancelCtrl, wxPanel)
319 bool OkCancelCtrl::Create(wxWindow* parent, wxWindowID id,
320 const wxPoint& pos, const wxSize& size, long style,
321 const wxString title)
323 if (!wxPanel::Create(parent, id, pos, size, style, title)) return false;
325 CreateControls();
326 GetSizer()->Fit(this);
327 GetSizer()->SetSizeHints(this);
328 return true;
331 void OkCancelCtrl::CreateControls()
333 // A top-level sizer
334 wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
335 this->SetSizer(topSizer);
337 // The OK button
338 m_OkBtn = new wxButton ( this, wxID_OK, wxT("&OK"),
339 wxDefaultPosition, wxDefaultSize, 0 );
340 topSizer->Add(m_OkBtn, 0, wxALL, 5);
341 // The Cancel button
342 m_CancelBtn = new wxButton ( this, wxID_CANCEL,
343 wxT("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
344 topSizer->Add(m_CancelBtn, 0, wxALL, 5);
346 Layout();
351 /////////////////////////////////////////////
352 //// Device Selector
353 //////////////////////////////////////////////
355 BEGIN_EVENT_TABLE(DeviceSelectorCtrl, wxPanel)
356 EVT_BUTTON(ID_AUTODETECT_BTN, DeviceSelectorCtrl::OnAutoDetect)
357 EVT_COMBOBOX(ID_DEVICE_CBX,DeviceSelectorCtrl::OnComboBox)
358 END_EVENT_TABLE()
360 IMPLEMENT_DYNAMIC_CLASS(DeviceSelectorCtrl, wxPanel)
362 bool DeviceSelectorCtrl::Create(wxWindow* parent, wxWindowID id,
363 const wxPoint& pos, const wxSize& size, long style,
364 const wxString title)
366 if (!wxPanel::Create(parent, id, pos, size, style, title)) return false;
368 CreateControls();
369 GetSizer()->Fit(this);
370 GetSizer()->SetSizeHints(this);
371 return true;
374 void DeviceSelectorCtrl::CreateControls()
376 // A top-level sizer
377 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
378 this->SetSizer(topSizer);
380 //Device Selection
381 wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
382 topSizer->Add(horizontalSizer, 0, wxALIGN_LEFT|wxALL, 5);
383 m_desc = new wxStaticText( this, wxID_STATIC,
384 wxT("Device:"), wxDefaultPosition,
385 wxDefaultSize, 0 );
386 horizontalSizer->Add(m_desc, 0, wxALIGN_LEFT|wxALL, 5);
388 m_deviceCbx = new wxComboBox(this, ID_DEVICE_CBX,wxT("Select your Device"),
389 wxDefaultPosition,wxDefaultSize,gv->plat_name,wxCB_READONLY);
391 m_deviceCbx->SetToolTip(wxT("Select your Device."));
392 m_deviceCbx->SetHelpText(wxT("Select your Device."));
394 horizontalSizer->Add(m_deviceCbx, 0, wxALIGN_LEFT|wxALL, 5);
396 wxButton* m_autodetectBtn = new wxButton(this, ID_AUTODETECT_BTN, wxT("Autodetect"),
397 wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
398 wxT("AutodetectBtn"));
400 m_autodetectBtn->SetToolTip(wxT("Click here to autodetect your Device."));
401 m_autodetectBtn->SetHelpText(wxT("Autodetection of the Device."));
402 // m_autodetectBtn->SetFocus();
404 horizontalSizer->Add(m_autodetectBtn,0,wxGROW | wxALL,5);
405 Layout();
409 wxString DeviceSelectorCtrl::getDevice()
411 return m_currentDevice;
414 void DeviceSelectorCtrl::setDefault()
416 int index = gv->plat_id.Index(gv->curplat);
417 if(index == -1) return;
418 m_deviceCbx->SetValue(gv->plat_name[index]);
421 void DeviceSelectorCtrl::OnComboBox(wxCommandEvent& event)
423 int index = gv->plat_name.Index(m_deviceCbx->GetValue());
425 if(index == -1)
427 m_currentDevice = wxT("");
428 return;
431 gv->curplat = gv->plat_id[index];
434 void DeviceSelectorCtrl::OnAutoDetect(wxCommandEvent& event)
436 AutoDetect();
440 void DeviceSelectorCtrl::AutoDetect()
444 UsbDeviceInfo device = detectDevicesViaPatchers();
446 if( device.status == NODEVICE)
448 WARN_DIALOG(wxT("No Device detected. (This function currently only works for Ipods and Sansas)."),
449 wxT("Detecting a Device"));
450 return;
453 if( device.status == TOMANYDEVICES)
455 WARN_DIALOG(wxT("More then one device detected, please connect only One"),
456 wxT("Detecting a Device"));
457 return;
461 if (device.status == 0 ) /* everything is ok */
463 m_deviceCbx->SetValue(gv->plat_name[device.device_index]);
464 gv->curplat=gv->plat_id[device.device_index];
466 if(device.path != wxT(""))
468 gv->curdestdir = device.path;
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);