Move the old api out of the core and into the plugin lib.
[Rockbox.git] / rbutil / rbutilCtrls.cpp
blob6556466d68b627734ffd70d976ca178c67e90ee7
2 #include "rbutilCtrls.h"
3 #include "bootloaders.h"
5 /////////////////////////////////////////////////////////////
6 //// Controls
7 ////////////////////////////////////////////////////////////////
9 /////////////////////////////////////////////
10 //// Image Ctrl
11 //////////////////////////////////////////////
13 BEGIN_EVENT_TABLE(ImageCtrl, wxControl)
14 EVT_PAINT(ImageCtrl::OnPaint)
15 END_EVENT_TABLE()
17 IMPLEMENT_DYNAMIC_CLASS(ImageCtrl, wxControl)
19 bool ImageCtrl::Create(wxWindow* parent, wxWindowID id,
20 const wxPoint& pos, const wxSize& size, long style,
21 const wxValidator& validator)
23 if (!wxControl::Create(parent, id, pos, size, style, validator)) return false;
25 return true;
28 void ImageCtrl::OnPaint(wxPaintEvent& event)
30 wxPaintDC dc(this);
31 dc.DrawBitmap(m_bitmap,0,0,false);
34 void ImageCtrl::SetBitmap(wxBitmap bmp)
36 m_bitmap = bmp;
37 Refresh();
41 wxSize ImageCtrl::DoGetBestSize() const
43 wxSize bestsize;
44 bestsize.x = m_bitmap.GetWidth();
45 bestsize.y = m_bitmap.GetHeight();
46 return bestsize;
51 /////////////////////////////////////////////
52 //// Theme Control
53 //////////////////////////////////////////////
55 BEGIN_EVENT_TABLE(ThemeCtrl, wxControl)
56 EVT_LISTBOX(ID_THEME_LST, ThemeCtrl::OnThemesLst)
57 END_EVENT_TABLE()
59 IMPLEMENT_DYNAMIC_CLASS(ThemeCtrl, wxControl)
61 bool ThemeCtrl::Create(wxWindow* parent, wxWindowID id,
62 const wxPoint& pos, const wxSize& size, long style,
63 const wxValidator& validator)
65 if (!wxControl::Create(parent, id, pos, size, style, validator)) return false;
67 CreateControls();
69 GetSizer()->Fit(this);
71 GetSizer()->SetSizeHints(this);
72 return true;
75 void ThemeCtrl::CreateControls()
77 // A top-level sizer
78 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
79 this->SetSizer(topSizer);
81 wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
82 topSizer->Add(horizontalSizer, 0, wxALIGN_LEFT|wxALL, 5);
84 //Device Selection
85 wxBoxSizer* wxBoxSizer7 = new wxBoxSizer(wxVERTICAL);
86 horizontalSizer->Add(wxBoxSizer7,0,wxGROW | wxALL,0);
88 wxStaticText* m_desc = new wxStaticText( this, wxID_STATIC,
89 wxT("Select one or more Themes to install"), wxDefaultPosition,
90 wxDefaultSize, 0 );
91 wxBoxSizer7->Add(m_desc, 0, wxALIGN_LEFT|wxALL, 5);
93 m_themeList = new wxListBox(this,ID_THEME_LST,wxDefaultPosition,
94 wxDefaultSize,0,NULL,wxLB_EXTENDED);
95 wxBoxSizer7->Add(m_themeList, 0, wxALIGN_LEFT|wxALL, 5);
97 // Preview Picture
98 wxBoxSizer* wxBoxSizer9 = new wxBoxSizer(wxVERTICAL);
99 horizontalSizer->Add(wxBoxSizer9,0,wxGROW | wxALL,0);
101 wxStaticText* preview_desc= new wxStaticText(this,wxID_ANY,wxT("Preview:"));
102 wxBoxSizer9->Add(preview_desc,0,wxGROW | wxALL,5);
104 m_PreviewBitmap = new ImageCtrl(this,ID_PREVIEW_BITMAP );
105 wxBoxSizer9->Add(m_PreviewBitmap,0,wxALIGN_LEFT | wxALL,5);
107 wxStaticBox* groupbox= new wxStaticBox(this,wxID_ANY,wxT("Selected Theme:"));
108 wxStaticBoxSizer* styleSizer = new wxStaticBoxSizer( groupbox, wxVERTICAL );
109 topSizer->Add(styleSizer,0,wxGROW|wxALL,0);
111 // horizontal sizer
112 wxBoxSizer* wxBoxSizer8 = new wxBoxSizer(wxHORIZONTAL);
113 styleSizer->Add(wxBoxSizer8,0,wxGROW | wxALL,0);
115 // File size
116 wxStaticText* size_desc= new wxStaticText(this,wxID_ANY,wxT("Filesize:"));
117 wxBoxSizer8->Add(size_desc,0,wxGROW | wxALL,5);
119 m_size= new wxStaticText(this,ID_FILESIZE,wxT(""));
120 wxBoxSizer8->Add(m_size,0,wxGROW | wxALL,5);
122 // Description
123 wxStaticText* desc_desc= new wxStaticText(this,wxID_ANY,wxT("Description:"));
124 styleSizer->Add(desc_desc,0,wxGROW | wxALL,5);
126 m_themedesc= new wxStaticText(this,ID_DESC,wxT(""));
127 styleSizer->Add(m_themedesc,0,wxGROW | wxALL,5);
129 topSizer->Fit(this);
130 topSizer->SetSizeHints(this);
131 Layout();
135 void ThemeCtrl::Init()
137 m_Themes.Clear();
138 m_Themes_path.Clear();
139 m_Themes_size.Clear();
140 m_Themes_image.Clear();
141 m_Themes_desc.Clear();
145 void ThemeCtrl::setDevice(wxString device)
148 int index = gv->plat_id.Index(device);
149 if(index == -1) return;
151 if(gv->plat_resolution[index] == m_currentResolution)
152 return;
153 else
154 m_currentResolution = gv->plat_resolution[index];
156 // load the themelist
157 Init();
158 m_size->SetLabel(wxT(""));
159 m_themedesc->SetLabel(wxT(""));
160 m_themeList->Clear();
162 //get correct Themes list
163 wxString src,dest,err;
165 src.Printf(wxT("%srbutil.php?res=%s"),gv->themes_url.c_str(),m_currentResolution.c_str());
166 dest.Printf(wxT("%s" PATH_SEP "download" PATH_SEP "%s.list"),
167 gv->stdpaths->GetUserDataDir().c_str(),m_currentResolution.c_str());
169 if(DownloadURL(src, dest))
171 MESG_DIALOG(wxT("Unable to download themes list."));
172 return;
175 //read and parse Themes list
176 wxString themelistraw;
177 wxFFile themefile;
178 if(!themefile.Open(dest)) //open file
180 MESG_DIALOG(wxT("Unable to open themes list."));
181 return;
183 if(!themefile.ReadAll(&themelistraw)) //read complete file
185 MESG_DIALOG(wxT("Unable to read themes list."));
186 return;
188 wxRegEx reAll(wxT("<body >(.+)</body>")); //extract body part
189 if(! reAll.Matches(themelistraw))
191 MESG_DIALOG(wxT("Themes list is in wrong Format."));
192 return;
194 wxString lines = reAll.GetMatch(themelistraw,1);
196 // prepare text
197 lines.Replace(wxT("<br />"),wxT(""),true); //replace <br /> with nothing
198 lines.Replace(wxT("\n"),wxT(""),true); //replace \n with nothing
199 lines.Trim(true); //strip WS at end
200 lines.Trim(false); //strip WS at beginning
201 wxStringTokenizer tkz(lines,wxT("|")); //tokenize it
203 while ( tkz.HasMoreTokens() ) // read all entrys
205 m_Themes.Add(tkz.GetNextToken()); //Theme name
206 m_Themes_path.Add(tkz.GetNextToken()); //Theme path
207 m_Themes_size.Add(tkz.GetNextToken()); //File size
208 m_Themes_image.Add(tkz.GetNextToken()); //Screenshot
209 m_Themes_desc.Add(tkz.GetNextToken()); //Description
211 m_themeList->Append(m_Themes.Last());
214 this->GetSizer()->Layout();
215 this->GetSizer()->Fit(this);
216 this->GetSizer()->SetSizeHints(this);
217 m_parent->GetSizer()->Layout();
218 m_parent->GetSizer()->Fit(m_parent);
219 m_parent->GetSizer()->SetSizeHints(m_parent);
223 void ThemeCtrl::OnThemesLst(wxCommandEvent& event)
225 // wxCriticalSectionLocker locker(m_ThemeSelectSection);
227 wxArrayInt selected;
228 int numSelected = m_themeList->GetSelections(selected);
229 if(numSelected == 0) return;
231 int index = selected[0];
233 m_size->SetLabel(m_Themes_size[index]);
234 m_themedesc->SetLabel(m_Themes_desc[index]);
235 m_themedesc->Wrap(200); // wrap desc
237 wxString src,dest;
239 int pos = m_Themes_image[index].Find('/',true);
240 wxString filename = m_Themes_image[index](pos+1,m_Themes_image[index].Length());
242 dest.Printf(wxT("%s" PATH_SEP "download" PATH_SEP "%s"),
243 gv->stdpaths->GetUserDataDir().c_str(),m_currentResolution.c_str());
245 if(!wxDirExists(dest))
246 wxMkdir(dest);
248 //this is a URL no PATH_SEP
249 src.Printf(wxT("%s/data/%s/%s"),gv->themes_url.c_str(),m_currentResolution.c_str(),filename.c_str());
250 dest.Printf(wxT("%s" PATH_SEP "download" PATH_SEP "%s" PATH_SEP "%s"),
251 gv->stdpaths->GetUserDataDir().c_str(),m_currentResolution.c_str(),filename.c_str());
253 if(DownloadURL(src, dest))
255 MESG_DIALOG(wxT("Unable to download image."));
256 return;
259 m_currentimage = dest;
260 wxBitmap bmp;
261 bmp.LoadFile(m_currentimage,wxBITMAP_TYPE_PNG);
262 m_PreviewBitmap->SetBitmap(bmp);
264 Refresh();
265 this->GetSizer()->Layout();
266 this->GetSizer()->Fit(this);
267 this->GetSizer()->SetSizeHints(this);
269 m_parent->GetSizer()->Layout();
270 m_parent->GetSizer()->Fit(m_parent);
271 m_parent->GetSizer()->SetSizeHints(m_parent);
276 wxArrayString ThemeCtrl::getThemesToInstall()
278 wxArrayString themes;
279 wxArrayInt selected;
280 int numSelected = m_themeList->GetSelections(selected);
282 for(int i=0; i < numSelected; i++)
284 themes.Add(m_Themes_path[selected[i]]);
286 return themes;
290 /////////////////////////////////////////////
291 //// Ok Cancel Control
292 //////////////////////////////////////////////
294 BEGIN_EVENT_TABLE(OkCancelCtrl, wxControl)
296 END_EVENT_TABLE()
298 IMPLEMENT_DYNAMIC_CLASS(OkCancelCtrl, wxControl)
300 bool OkCancelCtrl::Create(wxWindow* parent, wxWindowID id,
301 const wxPoint& pos, const wxSize& size, long style,
302 const wxValidator& validator)
304 if (!wxControl::Create(parent, id, pos, size, style, validator)) return false;
306 CreateControls();
307 GetSizer()->Fit(this);
308 GetSizer()->SetSizeHints(this);
309 return true;
312 void OkCancelCtrl::CreateControls()
314 // A top-level sizer
315 wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
316 this->SetSizer(topSizer);
318 // The OK button
319 m_OkBtn = new wxButton ( this, wxID_OK, wxT("&OK"),
320 wxDefaultPosition, wxDefaultSize, 0 );
321 topSizer->Add(m_OkBtn, 0, wxALL, 5);
322 // The Cancel button
323 m_CancelBtn = new wxButton ( this, wxID_CANCEL,
324 wxT("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
325 topSizer->Add(m_CancelBtn, 0, wxALL, 5);
327 Layout();
332 /////////////////////////////////////////////
333 //// Device Selector
334 //////////////////////////////////////////////
336 BEGIN_EVENT_TABLE(DeviceSelectorCtrl, wxControl)
337 EVT_BUTTON(ID_AUTODETECT_BTN, DeviceSelectorCtrl::OnAutoDetect)
338 EVT_COMBOBOX(ID_DEVICE_CBX,DeviceSelectorCtrl::OnComboBox)
339 END_EVENT_TABLE()
341 IMPLEMENT_DYNAMIC_CLASS(DeviceSelectorCtrl, wxControl)
343 bool DeviceSelectorCtrl::Create(wxWindow* parent, wxWindowID id,
344 const wxPoint& pos, const wxSize& size, long style,
345 const wxValidator& validator)
347 if (!wxControl::Create(parent, id, pos, size, style, validator)) return false;
349 CreateControls();
350 GetSizer()->Fit(this);
351 GetSizer()->SetSizeHints(this);
352 return true;
355 void DeviceSelectorCtrl::CreateControls()
357 // A top-level sizer
358 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
359 this->SetSizer(topSizer);
361 //Device Selection
363 wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
364 topSizer->Add(horizontalSizer, 0, wxALIGN_LEFT|wxALL, 5);
365 m_desc = new wxStaticText( this, wxID_STATIC,
366 wxT("Device:"), wxDefaultPosition,
367 wxDefaultSize, 0 );
368 horizontalSizer->Add(m_desc, 0, wxALIGN_LEFT|wxALL, 5);
370 m_deviceCbx = new wxComboBox(this, ID_DEVICE_CBX,wxT(""),
371 wxDefaultPosition,wxDefaultSize,gv->plat_name,wxCB_READONLY);
373 m_deviceCbx->SetToolTip(wxT("Select your Device."));
374 m_deviceCbx->SetHelpText(wxT("Select your Device."));
376 horizontalSizer->Add(m_deviceCbx, 0, wxALIGN_LEFT|wxALL, 5);
378 wxButton* m_autodetectBtn = new wxButton(this, ID_AUTODETECT_BTN, wxT("Autodetect"),
379 wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
380 wxT("AutodetectBtn"));
382 m_autodetectBtn->SetToolTip(wxT("Autodetection of the Device."));
383 m_autodetectBtn->SetHelpText(wxT("Autodetection of the Device."));
385 horizontalSizer->Add(m_autodetectBtn,0,wxGROW | wxALL,5);
386 Layout();
390 wxString DeviceSelectorCtrl::getDevice()
392 return m_currentDevice;
395 void DeviceSelectorCtrl::setDefault()
397 int index = gv->plat_id.Index(gv->curplat);
398 if(index == -1) return;
399 m_deviceCbx->SetValue(gv->plat_name[index]);
402 void DeviceSelectorCtrl::OnComboBox(wxCommandEvent& event)
404 int index = gv->plat_name.Index(m_deviceCbx->GetValue());
405 if(index == -1)
407 m_currentDevice = wxT("");
408 return;
411 gv->curplat = gv->plat_id[index];
414 void DeviceSelectorCtrl::OnAutoDetect(wxCommandEvent& event)
416 struct ipod_t ipod;
417 int n = ipod_scan(&ipod);
418 if(n == 1)
420 wxString temp(ipod.targetname,wxConvUTF8);
421 int index = gv->plat_bootloadername.Index(temp); // use the bootloader names..
422 m_deviceCbx->SetValue(gv->plat_name[index]);
423 gv->curplat=gv->plat_id[index];
424 return;
426 else if (n > 1)
428 WARN_DIALOG(wxT("More then one Ipod device detected, please connect only One"),
429 wxT("Detecting a Device"));
430 return;
433 struct sansa_t sansa;
434 int n2 = sansa_scan(&sansa);
435 if(n2==1)
437 int index = gv->plat_id.Index(wxT("sansae200"));
438 m_deviceCbx->SetValue(gv->plat_name[index]);
439 gv->curplat=gv->plat_id[index];
440 return;
442 else if (n2 > 1)
444 WARN_DIALOG(wxT("More then one Sansa device detected, please connect only One"),
445 wxT("Detecting a Device"));
446 return;
449 WARN_DIALOG(wxT("No Device detected. (This function currently only works for Ipods and Sansas)."),
450 wxT("Detecting a Device"));
451 return;
455 /////////////////////////////////////////////
456 //// DevicePosition Selector
457 //////////////////////////////////////////////
459 BEGIN_EVENT_TABLE(DevicePositionCtrl, wxControl)
460 EVT_BUTTON(ID_BROWSE_BTN, DevicePositionCtrl::OnBrowseBtn)
461 END_EVENT_TABLE()
463 IMPLEMENT_DYNAMIC_CLASS(DevicePositionCtrl, wxControl)
465 bool DevicePositionCtrl::Create(wxWindow* parent, wxWindowID id,
466 const wxPoint& pos, const wxSize& size, long style,
467 const wxValidator& validator)
469 if (!wxControl::Create(parent, id, pos, size, style, validator)) return false;
471 CreateControls();
472 GetSizer()->Fit(this);
473 GetSizer()->SetSizeHints(this);
474 return true;
477 void DevicePositionCtrl::CreateControls()
479 // A top-level sizer
480 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
481 this->SetSizer(topSizer);
483 //Device Selection
484 m_desc = new wxStaticText( this, wxID_STATIC,
485 wxT("Select your Device in the Filesystem"), wxDefaultPosition,
486 wxDefaultSize, 0 );
487 topSizer->Add(m_desc, 0, wxALIGN_LEFT|wxALL, 5);
489 wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
490 topSizer->Add(horizontalSizer, 0, wxGROW|wxALL, 5);
492 m_devicePos = new wxTextCtrl(this,wxID_ANY,gv->curdestdir);
493 m_devicePos->SetToolTip(wxT("Select your Devicefolder"));
494 m_devicePos->SetHelpText(wxT("Select your Devicefolder"));
495 horizontalSizer->Add(m_devicePos,0,wxGROW | wxALL,5);
497 m_browseBtn = new wxButton(this, ID_BROWSE_BTN, wxT("Browse"),
498 wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
499 wxT("BrowseBtn"));
500 m_browseBtn->SetToolTip(wxT("Browse for your Device"));
501 m_browseBtn->SetHelpText(wxT("Browse for your Device"));
502 horizontalSizer->Add(m_browseBtn,0,wxGROW | wxALL,5);
504 topSizer->Fit(this);
505 Layout();
509 wxString DevicePositionCtrl::getDevicePos()
511 return m_devicePos->GetValue();
515 void DevicePositionCtrl::setDefault()
517 m_devicePos->SetValue(gv->curdestdir);
520 void DevicePositionCtrl::OnBrowseBtn(wxCommandEvent& event)
522 const wxString& temp = wxDirSelector(
523 wxT("Please select the location of your audio device"), gv->curdestdir);
525 if (!temp.empty())
527 m_devicePos->SetValue(temp);
532 /////////////////////////////////////////////
533 //// FirmwarePosition Selector
534 //////////////////////////////////////////////
536 BEGIN_EVENT_TABLE(FirmwarePositionCtrl, wxControl)
537 EVT_BUTTON(ID_BROWSE_BTN, FirmwarePositionCtrl::OnBrowseBtn)
538 END_EVENT_TABLE()
540 IMPLEMENT_DYNAMIC_CLASS(FirmwarePositionCtrl, wxControl)
542 bool FirmwarePositionCtrl::Create(wxWindow* parent, wxWindowID id,
543 const wxPoint& pos, const wxSize& size, long style,
544 const wxValidator& validator)
546 if (!wxControl::Create(parent, id, pos, size, style, validator)) return false;
548 CreateControls();
549 GetSizer()->Fit(this);
550 GetSizer()->SetSizeHints(this);
551 return true;
554 void FirmwarePositionCtrl::CreateControls()
556 // A top-level sizer
557 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
558 this->SetSizer(topSizer);
560 //Device Selection
561 m_desc = new wxStaticText( this, wxID_STATIC,
562 wxT("Select original Firmware form the Manufacturer"), wxDefaultPosition,
563 wxDefaultSize, 0 );
564 topSizer->Add(m_desc, 0, wxALIGN_LEFT|wxALL, 5);
566 wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
567 topSizer->Add(horizontalSizer, 0, wxALIGN_LEFT|wxALL, 5);
569 m_firmwarePos = new wxTextCtrl(this,wxID_ANY,gv->curdestdir);
570 m_firmwarePos->SetToolTip(wxT("Select the original Firmware"));
571 m_firmwarePos->SetHelpText(wxT("Select the original Firmware"));
572 horizontalSizer->Add(m_firmwarePos,0,wxGROW | wxALL,5);
574 m_browseBtn = new wxButton(this, ID_BROWSE_BTN, wxT("Browse"),
575 wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator,
576 wxT("BrowseBtn"));
577 m_browseBtn->SetToolTip(wxT("Browse for the original Firmware"));
578 m_browseBtn->SetHelpText(wxT("Browse for the original Firmware"));
579 horizontalSizer->Add(m_browseBtn,0,wxGROW | wxALL,5);
581 Layout();
585 wxString FirmwarePositionCtrl::getFirmwarePos()
587 return m_firmwarePos->GetValue();
591 void FirmwarePositionCtrl::setDefault()
593 m_firmwarePos->SetValue(gv->curfirmware);
596 void FirmwarePositionCtrl::OnBrowseBtn(wxCommandEvent& event)
598 wxString temp = wxFileSelector(
599 wxT("Please select the location of the original Firmware"), gv->curdestdir,wxT(""),wxT(""),wxT("*.hex"));
601 if (!temp.empty())
603 m_firmwarePos->SetValue(temp);