cmWXCacheProperty.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. // cmCacheProperty.cxx : implementation file
  14. //
  15. #include "cmWXCacheProperty.h"
  16. #include "cmSystemTools.h"
  17. #include "cmWXMainFrame.h"
  18. static int GetClientHeight(wxWindow* w)
  19. {
  20. wxSize size = w->GetSize();
  21. return size.GetHeight();
  22. }
  23. #define cmMAX(x, y) (((x)>(y))?(x):(y))
  24. cmCacheProperty::cmCacheProperty(cmMainFrame* mf, const std::string& name) : m_Name(name)
  25. {
  26. this->m_HelpString = "";
  27. this->m_Value = "";
  28. this->m_NewValue = true;
  29. this->m_Removed = false;
  30. this->m_ItemType = cmCacheProperty::NOTHING;
  31. this->m_MainFrame = mf;
  32. this->m_Advanced = false;
  33. this->m_KeyWindow = 0;
  34. this->m_ValueWindow = 0;
  35. this->m_TextControl = 0;
  36. }
  37. cmCacheProperty::~cmCacheProperty()
  38. {
  39. }
  40. int cmCacheProperty::Display(wxSizer* s, wxPanel* win)
  41. {
  42. int maxheight = 0;
  43. this->m_TextControl = 0;
  44. wxPanel* panel = new wxPanel(win, -1);
  45. wxPanel* panel1 = new wxPanel(panel, -1);
  46. wxBoxSizer* sizer = 0;
  47. wxColor bgcolor = panel->GetBackgroundColour();
  48. sizer = new wxBoxSizer(wxHORIZONTAL);
  49. wxBoxSizer* sizer1 = new wxBoxSizer(wxHORIZONTAL);
  50. wxStaticText* name = new wxStaticText(panel1, -1, this->m_Name.c_str());
  51. this->SetupMenu(name);
  52. this->SetupMenu(panel1);
  53. sizer1->Add(name, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
  54. maxheight = cmMAX(maxheight, ::GetClientHeight(panel1));
  55. sizer->Add(5, 5, 0);
  56. sizer->Add(panel1, 1, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
  57. panel1->SetAutoLayout( TRUE );
  58. panel1->SetSizer(sizer1);
  59. sizer1->Fit(panel1);
  60. sizer1->SetSizeHints(panel1);
  61. sizer1->Layout();
  62. panel->SetAutoLayout( TRUE );
  63. panel->SetSizer(sizer);
  64. sizer->Fit(panel);
  65. sizer->SetSizeHints(panel);
  66. sizer->Layout();
  67. wxControl* value = 0;
  68. if ( this->m_NewValue )
  69. {
  70. wxColor brightred = wxColor(252, 102, 100);
  71. panel->SetBackgroundColour(brightred);
  72. panel1->SetBackgroundColour(brightred);
  73. name->SetBackgroundColour(brightred);
  74. }
  75. else
  76. {
  77. panel->SetBackgroundColour(*wxWHITE);
  78. panel1->SetBackgroundColour(*wxWHITE);
  79. name->SetBackgroundColour(*wxWHITE);
  80. }
  81. this->m_KeyWindow = panel;
  82. panel = new wxPanel(win, -1);
  83. sizer = new wxBoxSizer(wxHORIZONTAL);
  84. panel->SetBackgroundColour(*wxWHITE);
  85. //panel->SetBackgroundColour(*wxGREEN)
  86. #ifdef __APPLE__
  87. wxColor buttoncolor = *wxWHITE;
  88. #else // __APPLE__
  89. wxColor buttoncolor = bgcolor;
  90. #endif // __APPLE__
  91. switch ( this->m_ItemType )
  92. {
  93. case cmCacheProperty::CHECKBOX:
  94. sizer->Add(5, 5, 0);
  95. value = new wxCheckBox(panel, -1, "");
  96. this->ConnectEvent(value, wxEVT_COMMAND_CHECKBOX_CLICKED,
  97. (wxObjectEventFunction) &cmMainFrame::OnPropertyChanged);
  98. this->SetupMenu(value);
  99. if ( strcmp(this->GetValue().c_str(), "ON") == 0 )
  100. {
  101. static_cast<wxCheckBox*>(value)->SetValue(true);
  102. }
  103. break;
  104. case cmCacheProperty::EDIT:
  105. value = new wxTextCtrl(panel, -1, this->m_Value.c_str());
  106. maxheight = cmMAX(maxheight, ::GetClientHeight(value));
  107. this->ConnectEvent(value, wxEVT_COMMAND_TEXT_UPDATED,
  108. (wxObjectEventFunction) &cmMainFrame::OnPropertyChanged);
  109. this->SetupMenu(value);
  110. break;
  111. case cmCacheProperty::FILE:
  112. sizer->Add(5, 5, 0);
  113. value = new wxStaticText(panel, -1, this->m_Value.c_str());
  114. maxheight = cmMAX(maxheight, ::GetClientHeight(value));
  115. sizer->Add(value, 1, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
  116. this->m_TextControl = value;
  117. this->SetupMenu(value);
  118. value = new wxButton(panel, -1, "...", wxDefaultPosition,
  119. wxSize(20, maxheight - 4));
  120. maxheight = cmMAX(maxheight, ::GetClientHeight(value));
  121. value->SetBackgroundColour(buttoncolor);
  122. sizer->Add(value, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
  123. this->ConnectEvent(value, wxEVT_COMMAND_BUTTON_CLICKED,
  124. (wxObjectEventFunction) &cmMainFrame::OnPropertyChanged);
  125. this->SetupMenu(value);
  126. value = 0;
  127. break;
  128. case cmCacheProperty::PATH:
  129. sizer->Add(5, 5, 0);
  130. value = new wxStaticText(panel, -1, this->m_Value.c_str());
  131. maxheight = cmMAX(maxheight, ::GetClientHeight(value));
  132. sizer->Add(value, 1, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
  133. this->m_TextControl = value;
  134. this->SetupMenu(value);
  135. value = new wxButton(panel, -1, "...", wxDefaultPosition,
  136. wxSize(20, maxheight - 4));
  137. maxheight = cmMAX(maxheight, ::GetClientHeight(value));
  138. value->SetBackgroundColour(buttoncolor);
  139. sizer->Add(value, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
  140. this->ConnectEvent(value, wxEVT_COMMAND_BUTTON_CLICKED,
  141. (wxObjectEventFunction) &cmMainFrame::OnPropertyChanged);
  142. this->SetupMenu(value);
  143. value = 0;
  144. break;
  145. default:
  146. value = new wxStaticText(panel, -1, this->m_Value.c_str());
  147. maxheight = cmMAX(maxheight, ::GetClientHeight(value));
  148. this->m_TextControl = value;
  149. break;
  150. }
  151. //panel->Fit();
  152. this->m_ValueWindow = panel;
  153. //panel->Fit();
  154. //win->Fit();
  155. if ( value )
  156. {
  157. sizer->Add(value, 1, wxALIGN_LEFT | wxGROW | wxALL | wxALIGN_CENTER_VERTICAL);
  158. }
  159. //s->Layout();
  160. panel->SetAutoLayout( TRUE );
  161. panel->SetSizer(sizer);
  162. sizer->Fit(panel);
  163. sizer->SetSizeHints(panel);
  164. sizer->Layout();
  165. if ( this->m_NewValue )
  166. {
  167. s->Prepend(this->m_ValueWindow, 1, wxGROW | wxLEFT | wxRIGHT );
  168. s->Prepend(this->m_KeyWindow, 1, wxGROW | wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL );
  169. }
  170. else
  171. {
  172. s->Add(this->m_KeyWindow, 1, wxGROW | wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL );
  173. s->Add(this->m_ValueWindow, 1, wxGROW | wxLEFT | wxRIGHT );
  174. }
  175. this->SetupMenu(this->m_KeyWindow);
  176. this->SetupMenu(this->m_ValueWindow);
  177. int x1, x2, xm, s1, s2, sm;
  178. win->GetSize(&xm, &sm);
  179. this->m_KeyWindow->GetSize(&x1, &s1);
  180. this->m_ValueWindow->GetSize(&x2, &s2);
  181. int m = s1;
  182. if ( s2 > m )
  183. {
  184. m = s2;
  185. }
  186. this->m_KeyWindow->SetSize(x1, m);
  187. this->m_ValueWindow->SetSize(x2, m);
  188. //std::cout << "Size of panels: " << sm << "," << s1 << ", " << s2 << " max: " << maxheight<< std::endl;
  189. return maxheight;
  190. }
  191. void cmCacheProperty::Remove(wxSizer* sizer, wxPanel*)
  192. {
  193. if ( this->m_KeyWindow )
  194. {
  195. sizer->Remove(this->m_KeyWindow);
  196. this->m_KeyWindow->Destroy();
  197. }
  198. if ( this->m_ValueWindow )
  199. {
  200. sizer->Remove(this->m_ValueWindow);
  201. this->m_ValueWindow->Destroy();
  202. }
  203. this->m_KeyWindow = 0;
  204. this->m_ValueWindow = 0;
  205. //sizer->Layout();
  206. //win->Fit();
  207. }
  208. void cmCacheProperty::ConnectEvent(wxWindow* win, wxEventType et, wxObjectEventFunction func)
  209. {
  210. if ( !this->m_MainFrame )
  211. {
  212. return;
  213. }
  214. win->SetClientData(this);
  215. this->m_MainFrame->Connect(win->GetId(), et, func);
  216. }
  217. void cmCacheProperty::ConnectEventTo(wxWindow* win, wxEventType et,
  218. wxObjectEventFunction func)
  219. {
  220. if ( !this->m_MainFrame )
  221. {
  222. return;
  223. }
  224. win->SetClientData(this);
  225. this->m_MainFrame->ConnectEventTo(win, et, func);
  226. }
  227. void cmCacheProperty::OnPropertyChanged(wxEvent& event)
  228. {
  229. if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
  230. {
  231. }
  232. else
  233. {
  234. switch ( this->m_ItemType )
  235. {
  236. case cmCacheProperty::EDIT: this->OnEntryChanged(event); break;
  237. case cmCacheProperty::FILE: this->OnFileBrowseButton(event); break;
  238. case cmCacheProperty::CHECKBOX: this->OnCheckboxButton(event); break;
  239. case cmCacheProperty::PATH: this->OnPathBrowseButton(event); break;
  240. }
  241. }
  242. }
  243. void cmCacheProperty::OnFileBrowseButton(wxEvent&)
  244. {
  245. std::string path = cmSystemTools::GetFilenamePath(this->m_Value);
  246. std::string file = cmSystemTools::GetFilenameName(this->m_Value);
  247. if ( path == "NOTFOUND" )
  248. {
  249. path = this->m_MainFrame->GetBuildDir();
  250. }
  251. wxFileDialog dialog (
  252. this->m_MainFrame,
  253. _T("Select file"),
  254. path.c_str(),
  255. file.c_str(),
  256. _T("All files|*.*")
  257. );
  258. if (dialog.ShowModal() == wxID_OK)
  259. {
  260. std::string str = "";
  261. if ( this->m_TextControl )
  262. {
  263. str += dialog.GetPath().c_str();
  264. static_cast<wxStaticText*>(this->m_TextControl)->SetLabel(str.c_str());
  265. }
  266. this->SetValue(str.c_str());
  267. }
  268. }
  269. void cmCacheProperty::OnPathBrowseButton(wxEvent&)
  270. {
  271. std::string path = this->m_Value;
  272. if ( path == "NOTFOUND" )
  273. {
  274. path = this->m_MainFrame->GetBuildDir();
  275. }
  276. wxDirDialog dialog ( this->m_MainFrame, _T("Select directory"), path.c_str() );
  277. if (dialog.ShowModal() == wxID_OK)
  278. {
  279. if ( this->m_TextControl )
  280. {
  281. static_cast<wxStaticText*>(this->m_TextControl)->SetLabel(dialog.GetPath());
  282. }
  283. this->SetValue(dialog.GetPath().c_str());
  284. }
  285. }
  286. void cmCacheProperty::OnCheckboxButton(wxEvent& event)
  287. {
  288. wxCheckBox* widget = static_cast<wxCheckBox*>( event.GetEventObject() );
  289. if ( !widget )
  290. {
  291. return;
  292. }
  293. int val = widget->GetValue();
  294. if ( val )
  295. {
  296. this->SetValue("ON");
  297. }
  298. else
  299. {
  300. this->SetValue("OFF");
  301. }
  302. }
  303. void cmCacheProperty::OnEntryChanged(wxEvent& event)
  304. {
  305. wxTextCtrl* widget = static_cast<wxTextCtrl*>( event.GetEventObject() );
  306. if ( !widget )
  307. {
  308. return;
  309. }
  310. this->SetValue(static_cast<const char*>(widget->GetValue()));
  311. }
  312. void cmCacheProperty::SetupMenu(wxWindow* win)
  313. {
  314. this->ConnectEventTo(win, wxEVT_RIGHT_DOWN,
  315. (wxObjectEventFunction) &cmMainFrame::OnPopupMenu);
  316. this->ConnectEventTo(win, wxEVT_MOTION,
  317. (wxObjectEventFunction) &cmMainFrame::OnCacheStatusBar);
  318. }