PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wxWidgets-2.8.12/src/mac/classic/radiobox.cpp

#
C++ | 530 lines | 331 code | 97 blank | 102 comment | 49 complexity | 89b9bf40e443c5634785bc8506191c55 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-3.0
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: src/mac/classic/radiobox.cpp
  3. // Purpose: wxRadioBox
  4. // Author: Stefan Csomor
  5. // Modified by: JS Lair (99/11/15) first implementation
  6. // Created: 1998-01-01
  7. // RCS-ID: $Id: radiobox.cpp 39694 2006-06-13 11:30:40Z ABX $
  8. // Copyright: (c) Stefan Csomor
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. //-------------------------------------------------------------------------------------
  12. // headers
  13. //-------------------------------------------------------------------------------------
  14. #include "wx/wxprec.h"
  15. #if wxUSE_RADIOBOX
  16. #include "wx/radiobox.h"
  17. #ifndef WX_PRECOMP
  18. #include "wx/radiobut.h"
  19. #include "wx/arrstr.h"
  20. #endif
  21. #include "wx/mac/uma.h"
  22. IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
  23. //-------------------------------------------------------------------------------------
  24. // ? wxRadioBox()
  25. //-------------------------------------------------------------------------------------
  26. // Default constructor
  27. BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
  28. EVT_RADIOBUTTON( wxID_ANY , wxRadioBox::OnRadioButton )
  29. END_EVENT_TABLE()
  30. void wxRadioBox::OnRadioButton( wxCommandEvent &outer )
  31. {
  32. if ( outer.IsChecked() )
  33. {
  34. wxCommandEvent event(wxEVT_COMMAND_RADIOBOX_SELECTED, m_windowId);
  35. int i = GetSelection() ;
  36. event.SetInt( i );
  37. event.SetString(GetString(i));
  38. event.SetEventObject( this );
  39. ProcessCommand(event);
  40. }
  41. }
  42. wxRadioBox::wxRadioBox()
  43. {
  44. m_noItems = 0;
  45. m_noRowsOrCols = 0;
  46. m_radioButtonCycle = NULL;
  47. }
  48. //-------------------------------------------------------------------------------------
  49. // ? wxRadioBox(wxWindow*, wxWindowID, const wxString&, const wxPoint&,
  50. // const wxSize&, int, const wxString[], int, long,
  51. // const wxValidator&, const wxString&)
  52. //-------------------------------------------------------------------------------------
  53. // Contructor, creating and showing a radiobox
  54. //
  55. // inline defined
  56. //
  57. //-------------------------------------------------------------------------------------
  58. // ? ~wxRadioBox
  59. //-------------------------------------------------------------------------------------
  60. // Destructor, destroying the radiobox item
  61. wxRadioBox::~wxRadioBox()
  62. {
  63. m_isBeingDeleted = true;
  64. wxRadioButton *next,*current;
  65. current=m_radioButtonCycle->NextInCycle();
  66. next=current->NextInCycle();
  67. while (current!=m_radioButtonCycle) {
  68. delete current;
  69. current=next;
  70. next=current->NextInCycle();
  71. }
  72. delete current;
  73. }
  74. //-------------------------------------------------------------------------------------
  75. // ? Create
  76. //-------------------------------------------------------------------------------------
  77. // Create the radiobox for two-step construction
  78. bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
  79. const wxPoint& pos, const wxSize& size,
  80. const wxArrayString& choices,
  81. int majorDim, long style,
  82. const wxValidator& val, const wxString& name)
  83. {
  84. wxCArrayString chs(choices);
  85. return Create(parent, id, label, pos, size, chs.GetCount(),
  86. chs.GetStrings(), majorDim, style, val, name);
  87. }
  88. bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& label,
  89. const wxPoint& pos, const wxSize& size,
  90. int n, const wxString choices[],
  91. int majorDim, long style,
  92. const wxValidator& val, const wxString& name)
  93. {
  94. if ( !wxControl::Create(parent, id, pos, size, style, val, name) )
  95. return false;
  96. int i;
  97. m_noItems = (unsigned int)n;
  98. m_noRowsOrCols = majorDim;
  99. m_radioButtonCycle = NULL;
  100. SetMajorDim(majorDim == 0 ? n : majorDim, style);
  101. Rect bounds ;
  102. Str255 title ;
  103. MacPreControlCreate( parent , id , wxStripMenuCodes(label) , pos , size ,style, val , name , &bounds , title ) ;
  104. m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , 0 , 1,
  105. kControlGroupBoxTextTitleProc , (long) this ) ;
  106. for (i = 0; i < n; i++)
  107. {
  108. wxRadioButton *radBtn = new wxRadioButton
  109. (
  110. this,
  111. wxID_ANY,
  112. wxStripMenuCodes(choices[i]),
  113. wxPoint(5,20*i+10),
  114. wxDefaultSize,
  115. i == 0 ? wxRB_GROUP : 0
  116. );
  117. if ( i == 0 )
  118. m_radioButtonCycle = radBtn ;
  119. // m_radioButtonCycle=radBtn->AddInCycle(m_radioButtonCycle);
  120. }
  121. SetSelection(0);
  122. MacPostControlCreate() ;
  123. return true;
  124. }
  125. //-------------------------------------------------------------------------------------
  126. // ? Enable(bool)
  127. //-------------------------------------------------------------------------------------
  128. // Enables or disables the entire radiobox
  129. bool wxRadioBox::Enable(bool enable)
  130. {
  131. if (!wxControl::Enable(enable))
  132. return false;
  133. wxRadioButton *current = m_radioButtonCycle;
  134. for (unsigned int i = 0; i < m_noItems; i++)
  135. {
  136. current->Enable(enable);
  137. current = current->NextInCycle();
  138. }
  139. return true;
  140. }
  141. //-------------------------------------------------------------------------------------
  142. // ? Enable(unsigned int, bool)
  143. //-------------------------------------------------------------------------------------
  144. // Enables or disables an given button
  145. bool wxRadioBox::Enable(unsigned int item, bool enable)
  146. {
  147. if (!IsValid(item))
  148. return false;
  149. unsigned int i = 0;
  150. wxRadioButton *current = m_radioButtonCycle;
  151. while (i != item)
  152. {
  153. i++;
  154. current = current->NextInCycle();
  155. }
  156. return current->Enable(enable);
  157. }
  158. //-------------------------------------------------------------------------------------
  159. // ? GetLabel()
  160. //-------------------------------------------------------------------------------------
  161. // Returns the radiobox label
  162. wxString wxRadioBox::GetLabel() const
  163. {
  164. return wxControl::GetLabel();
  165. }
  166. //-------------------------------------------------------------------------------------
  167. // ? GetLabel(int)
  168. //-------------------------------------------------------------------------------------
  169. // Returns the label for the given button
  170. wxString wxRadioBox::GetString(unsigned int item) const
  171. {
  172. wxRadioButton *current;
  173. if (!IsValid(item))
  174. return wxEmptyString;
  175. unsigned int i = 0;
  176. current = m_radioButtonCycle;
  177. while (i != item) {
  178. i++;
  179. current = current->NextInCycle();
  180. }
  181. return current->GetLabel();
  182. }
  183. //-------------------------------------------------------------------------------------
  184. // ? GetSelection
  185. //-------------------------------------------------------------------------------------
  186. // Returns the zero-based position of the selected button
  187. int wxRadioBox::GetSelection() const
  188. {
  189. int i;
  190. wxRadioButton *current;
  191. i=0;
  192. current=m_radioButtonCycle;
  193. while (!current->GetValue()) {
  194. i++;
  195. current=current->NextInCycle();
  196. }
  197. return i;
  198. }
  199. //-------------------------------------------------------------------------------------
  200. // ? Number
  201. //-------------------------------------------------------------------------------------
  202. // Returns the number of buttons in the radiobox
  203. //
  204. // inline defined
  205. //
  206. //-------------------------------------------------------------------------------------
  207. // ? SetLabel(const wxString&)
  208. //-------------------------------------------------------------------------------------
  209. // Sets the radiobox label
  210. void wxRadioBox::SetLabel(const wxString& label)
  211. {
  212. return wxControl::SetLabel(label);
  213. }
  214. //-------------------------------------------------------------------------------------
  215. // ? SetLabel(int, const wxString&)
  216. //-------------------------------------------------------------------------------------
  217. // Sets the label of a given button
  218. void wxRadioBox::SetString(unsigned int item,const wxString& label)
  219. {
  220. if (!IsValid(item))
  221. return;
  222. unsigned int i=0;
  223. wxRadioButton *current=m_radioButtonCycle;
  224. while (i!=item)
  225. {
  226. i++;
  227. current=current->NextInCycle();
  228. }
  229. return current->SetLabel(label);
  230. }
  231. //-------------------------------------------------------------------------------------
  232. // ? SetSelection
  233. //-------------------------------------------------------------------------------------
  234. // Sets a button by passing the desired position. This does not cause
  235. // wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted
  236. void wxRadioBox::SetSelection(int item)
  237. {
  238. int i;
  239. wxRadioButton *current;
  240. if (!IsValid(item))
  241. return;
  242. i=0;
  243. current=m_radioButtonCycle;
  244. while (i!=item) {
  245. i++;
  246. current=current->NextInCycle();
  247. }
  248. current->SetValue(true);
  249. }
  250. //-------------------------------------------------------------------------------------
  251. // ? Show(bool)
  252. //-------------------------------------------------------------------------------------
  253. // Shows or hides the entire radiobox
  254. bool wxRadioBox::Show(bool show)
  255. {
  256. wxRadioButton *current;
  257. wxControl::Show(show);
  258. current=m_radioButtonCycle;
  259. for (unsigned int i=0; i<m_noItems; i++)
  260. {
  261. current->Show(show);
  262. current=current->NextInCycle();
  263. }
  264. return true;
  265. }
  266. //-------------------------------------------------------------------------------------
  267. // ? Show(unsigned int, bool)
  268. //-------------------------------------------------------------------------------------
  269. // Shows or hides the given button
  270. bool wxRadioBox::Show(unsigned int item, bool show)
  271. {
  272. if (!IsValid(item))
  273. return false;
  274. unsigned int i = 0;
  275. wxRadioButton *current=m_radioButtonCycle;
  276. while (i!=item) {
  277. i++;
  278. current=current->NextInCycle();
  279. }
  280. return current->Show(show);
  281. }
  282. //-------------------------------------------------------------------------------------
  283. // ? Command
  284. //-------------------------------------------------------------------------------------
  285. // Simulates the effect of the user issuing a command to the item
  286. void wxRadioBox::Command (wxCommandEvent & event)
  287. {
  288. SetSelection (event.GetInt());
  289. ProcessCommand (event);
  290. }
  291. //-------------------------------------------------------------------------------------
  292. // ? SetFocus
  293. //-------------------------------------------------------------------------------------
  294. // Sets the selected button to receive keyboard input
  295. void wxRadioBox::SetFocus()
  296. {
  297. int i;
  298. wxRadioButton *current;
  299. i=0;
  300. current=m_radioButtonCycle;
  301. while (!current->GetValue()) {
  302. i++;
  303. current=current->NextInCycle();
  304. }
  305. current->SetFocus();
  306. }
  307. //-------------------------------------------------------------------------------------
  308. // ? DoSetSize
  309. //-------------------------------------------------------------------------------------
  310. // Simulates the effect of the user issuing a command to the item
  311. #define RADIO_SIZE 20
  312. void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
  313. {
  314. unsigned int i;
  315. wxRadioButton *current;
  316. // define the position
  317. int x_current, y_current;
  318. int x_offset,y_offset;
  319. int widthOld, heightOld;
  320. GetSize(&widthOld, &heightOld);
  321. x_offset = x;
  322. y_offset = y;
  323. GetPosition(&x_current, &y_current);
  324. if ((x == wxDefaultCoord) && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
  325. x_offset = x_current;
  326. if ((y == wxDefaultCoord)&& !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
  327. y_offset = y_current;
  328. // define size
  329. int charWidth,charHeight;
  330. int maxWidth,maxHeight;
  331. int eachWidth[128],eachHeight[128];
  332. int totWidth,totHeight;
  333. SetFont(GetParent()->GetFont());
  334. GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &charWidth, &charHeight);
  335. charWidth/=52;
  336. maxWidth=-1;
  337. maxHeight=-1;
  338. for (i = 0 ; i < m_noItems; i++)
  339. {
  340. GetTextExtent(GetString(i), &eachWidth[i], &eachHeight[i]);
  341. eachWidth[i] = (int)(eachWidth[i] + RADIO_SIZE);
  342. eachHeight[i] = (int)((3*eachHeight[i])/2);
  343. if (maxWidth<eachWidth[i]) maxWidth = eachWidth[i];
  344. if (maxHeight<eachHeight[i]) maxHeight = eachHeight[i];
  345. }
  346. totHeight = GetRowCount() * (maxHeight + charHeight/2) + charHeight ;
  347. totWidth = GetColumnCount() * (maxWidth + charWidth) + charWidth;
  348. // only change our width/height if asked for
  349. if ( width == wxDefaultCoord )
  350. {
  351. if ( sizeFlags & wxSIZE_AUTO_WIDTH )
  352. width = totWidth ;
  353. else
  354. width = widthOld;
  355. }
  356. if ( height == wxDefaultCoord )
  357. {
  358. if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
  359. height = totHeight ;
  360. else
  361. height = heightOld;
  362. }
  363. wxControl::DoSetSize(x_offset,y_offset,width,height,wxSIZE_AUTO);
  364. // arrange radiobuttons
  365. int x_start,y_start;
  366. x_start = charWidth;
  367. y_start = 15 ;
  368. if ( UMAGetSystemVersion() >= 0x1030 )
  369. {
  370. //need to add a few more pixels for the top border on panther
  371. y_start = y_start + 5; //how many exactly should this be to meet the HIG?
  372. }
  373. x_offset = x_start;
  374. y_offset = y_start;
  375. current=m_radioButtonCycle;
  376. for ( i = 0 ; i < m_noItems; i++)
  377. {
  378. if (i&&((i%GetMajorDim())==0)) // not to do for the zero button!
  379. {
  380. if (m_windowStyle & wxRA_VERTICAL)
  381. {
  382. x_offset += maxWidth + charWidth;
  383. y_offset = y_start;
  384. }
  385. else
  386. {
  387. x_offset = x_start;
  388. y_offset += maxHeight ; /*+ charHeight/2;*/
  389. }
  390. }
  391. current->SetSize(x_offset,y_offset,eachWidth[i],eachHeight[i]);
  392. current=current->NextInCycle();
  393. if (m_windowStyle & wxRA_SPECIFY_ROWS)
  394. y_offset += maxHeight ; /*+ charHeight/2;*/
  395. else
  396. x_offset += maxWidth + charWidth;
  397. }
  398. }
  399. wxSize wxRadioBox::DoGetBestSize() const
  400. {
  401. int charWidth, charHeight;
  402. int maxWidth, maxHeight;
  403. int eachWidth, eachHeight;
  404. int totWidth, totHeight;
  405. wxFont font = GetParent()->GetFont();
  406. GetTextExtent(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
  407. &charWidth, &charHeight, NULL, NULL, &font);
  408. charWidth /= 52;
  409. maxWidth = -1;
  410. maxHeight = -1;
  411. for (unsigned int i = 0 ; i < m_noItems; i++)
  412. {
  413. GetTextExtent(GetString(i), &eachWidth, &eachHeight);
  414. eachWidth = (int)(eachWidth + RADIO_SIZE) ;
  415. eachHeight = (int)((3 * eachHeight) / 2);
  416. if (maxWidth < eachWidth) maxWidth = eachWidth;
  417. if (maxHeight < eachHeight) maxHeight = eachHeight;
  418. }
  419. totHeight = GetRowCount() * (maxHeight + charHeight/2) + charHeight ;
  420. totWidth = GetColumnCount() * (maxWidth + charWidth) + charWidth;
  421. if ( UMAGetSystemVersion() >= 0x1030 )
  422. {
  423. //need to add a few more pixels for the static boxborder on panther
  424. totHeight = totHeight + 10; //how many exactly should this be to meet the HIG?
  425. }
  426. // handle radio box title as well
  427. GetTextExtent(GetLabel(), &eachWidth, NULL);
  428. eachWidth = (int)(eachWidth + RADIO_SIZE) + 3 * charWidth ;
  429. if (totWidth < eachWidth)
  430. totWidth = eachWidth;
  431. return wxSize(totWidth, totHeight);
  432. }
  433. #endif // wxUSE_RADIOBOX