| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /***************************************************************************** |
| 2 | * Module for Microchip Graphics Library |
||
| 3 | * GOL Layer |
||
| 4 | * Radio Button |
||
| 5 | ***************************************************************************** |
||
| 6 | * FileName: RadioButton.c |
||
| 7 | * Dependencies: Graphics.h |
||
| 8 | * Processor: PIC24F, PIC24H, dsPIC, PIC32 |
||
| 9 | * Compiler: MPLAB C30 V3.00, MPLAB C32 |
||
| 10 | * Linker: MPLAB LINK30, MPLAB LINK32 |
||
| 11 | * Company: Microchip Technology Incorporated |
||
| 12 | * |
||
| 13 | * Software License Agreement |
||
| 14 | * |
||
| 15 | * Copyright © 2008 Microchip Technology Inc. All rights reserved. |
||
| 16 | * Microchip licenses to you the right to use, modify, copy and distribute |
||
| 17 | * Software only when embedded on a Microchip microcontroller or digital |
||
| 18 | * signal controller, which is integrated into your product or third party |
||
| 19 | * product (pursuant to the sublicense terms in the accompanying license |
||
| 20 | * agreement). |
||
| 21 | * |
||
| 22 | * You should refer to the license agreement accompanying this Software |
||
| 23 | * for additional information regarding your rights and obligations. |
||
| 24 | * |
||
| 25 | * SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY |
||
| 26 | * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY |
||
| 27 | * OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR |
||
| 28 | * PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR |
||
| 29 | * OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, |
||
| 30 | * BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT |
||
| 31 | * DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, |
||
| 32 | * INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, |
||
| 33 | * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY |
||
| 34 | * CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), |
||
| 35 | * OR OTHER SIMILAR COSTS. |
||
| 36 | * |
||
| 37 | * Author Date Comment |
||
| 38 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 39 | * Anton Alkhimenok 11/12/07 Version 1.0 release |
||
| 40 | *****************************************************************************/ |
||
| 41 | #include "Graphics\Graphics.h" |
||
| 42 | |||
| 43 | #ifdef USE_RADIOBUTTON |
||
| 44 | |||
| 45 | // This pointer is used to create linked list of radio buttons for the group |
||
| 46 | RADIOBUTTON *_pListButtons = NULL; |
||
| 47 | |||
| 48 | /********************************************************************* |
||
| 49 | * Function: RADIOBUTTON *RbCreate(WORD ID, SHORT left, SHORT top, SHORT right, |
||
| 50 | * SHORT bottom, WORD state, XCHAR *pText, GOL_SCHEME *pScheme) |
||
| 51 | * |
||
| 52 | * Overview: creates the radio button |
||
| 53 | * |
||
| 54 | ********************************************************************/ |
||
| 55 | RADIOBUTTON *RbCreate |
||
| 56 | ( |
||
| 57 | WORD ID, |
||
| 58 | SHORT left, |
||
| 59 | SHORT top, |
||
| 60 | SHORT right, |
||
| 61 | SHORT bottom, |
||
| 62 | WORD state, |
||
| 63 | XCHAR *pText, |
||
| 64 | GOL_SCHEME *pScheme |
||
| 65 | ) |
||
| 66 | { |
||
| 67 | RADIOBUTTON *pRb = NULL; |
||
| 68 | RADIOBUTTON *pointer; |
||
| 69 | |||
| 70 | pRb = (RADIOBUTTON *)GFX_malloc(sizeof(RADIOBUTTON)); |
||
| 71 | if(pRb == NULL) |
||
| 72 | return (pRb); |
||
| 73 | |||
| 74 | pRb->hdr.ID = ID; |
||
| 75 | pRb->hdr.pNxtObj = NULL; |
||
| 76 | pRb->hdr.type = OBJ_RADIOBUTTON; |
||
| 77 | pRb->hdr.left = left; |
||
| 78 | pRb->hdr.top = top; |
||
| 79 | pRb->hdr.right = right; |
||
| 80 | pRb->hdr.bottom = bottom; |
||
| 81 | pRb->pText = pText; |
||
| 82 | pRb->pNext = NULL; // last radio button in the list |
||
| 83 | pRb->hdr.state = state; |
||
| 84 | |||
| 85 | if(GetState(pRb, RB_GROUP)) |
||
| 86 | { |
||
| 87 | |||
| 88 | // If it's first button in the group start new button's list |
||
| 89 | _pListButtons = pRb; |
||
| 90 | |||
| 91 | // Attach the button to the list |
||
| 92 | pRb->pHead = (OBJ_HEADER *)_pListButtons; |
||
| 93 | } |
||
| 94 | else |
||
| 95 | { |
||
| 96 | |||
| 97 | // Attach the button to the list |
||
| 98 | pRb->pHead = (OBJ_HEADER *)_pListButtons; |
||
| 99 | pointer = _pListButtons; |
||
| 100 | while(pointer->pNext != NULL) |
||
| 101 | { |
||
| 102 | pointer = (RADIOBUTTON *)pointer->pNext; |
||
| 103 | } |
||
| 104 | |||
| 105 | pointer->pNext = (OBJ_HEADER *)pRb; |
||
| 106 | } |
||
| 107 | |||
| 108 | // Set the style scheme to be used |
||
| 109 | if(pScheme == NULL) |
||
| 110 | pRb->hdr.pGolScheme = _pDefaultGolScheme; |
||
| 111 | else |
||
| 112 | pRb->hdr.pGolScheme = pScheme; |
||
| 113 | |||
| 114 | // Set the text height |
||
| 115 | pRb->textHeight = 0; |
||
| 116 | if(pText != NULL) |
||
| 117 | { |
||
| 118 | pRb->textHeight = GetTextHeight(pRb->hdr.pGolScheme->pFont); |
||
| 119 | } |
||
| 120 | |||
| 121 | GOLAddObject((OBJ_HEADER *)pRb); |
||
| 122 | |||
| 123 | return (pRb); |
||
| 124 | } |
||
| 125 | |||
| 126 | /********************************************************************* |
||
| 127 | * Function: void RbSetCheck(RADIOBUTTON *pRb, WORD ID) |
||
| 128 | * |
||
| 129 | * Input: pRb - the pointer to any radio button in the group |
||
| 130 | * ID - ID of button to be checked |
||
| 131 | * |
||
| 132 | * Output: none |
||
| 133 | * |
||
| 134 | * Overview: sets radio button to checked state, and marks states of group |
||
| 135 | * radio buttons to be redrawn |
||
| 136 | * |
||
| 137 | ********************************************************************/ |
||
| 138 | void RbSetCheck(RADIOBUTTON *pRb, WORD ID) |
||
| 139 | { |
||
| 140 | RADIOBUTTON *pointer; |
||
| 141 | |||
| 142 | pointer = (RADIOBUTTON *)pRb->pHead; |
||
| 143 | |||
| 144 | while(pointer != NULL) |
||
| 145 | { |
||
| 146 | if(pointer->hdr.ID == ID) |
||
| 147 | { |
||
| 148 | SetState(pointer, RB_CHECKED | RB_DRAW_CHECK); // set check and redraw |
||
| 149 | } |
||
| 150 | else |
||
| 151 | { |
||
| 152 | ClrState(pointer, RB_CHECKED); // reset checked |
||
| 153 | SetState(pointer, RB_DRAW_CHECK); // redraw |
||
| 154 | } |
||
| 155 | |||
| 156 | pointer = (RADIOBUTTON *)pointer->pNext; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /********************************************************************* |
||
| 161 | * Function: WORD RbGetCheck(RADIOBUTTON *pRb) |
||
| 162 | * |
||
| 163 | * Input: pRb - the pointer to any radio button in the group |
||
| 164 | * |
||
| 165 | * Output: ID of checked button, -1 if there are no checked buttons |
||
| 166 | * |
||
| 167 | * Overview: gets ID of checked radio button |
||
| 168 | * |
||
| 169 | ********************************************************************/ |
||
| 170 | WORD RbGetCheck(RADIOBUTTON *pRb) |
||
| 171 | { |
||
| 172 | RADIOBUTTON *pointer; |
||
| 173 | |||
| 174 | pointer = (RADIOBUTTON *)pRb->pHead; |
||
| 175 | |||
| 176 | while(pointer != NULL) |
||
| 177 | { |
||
| 178 | if(GetState(pointer, RB_CHECKED)) |
||
| 179 | { |
||
| 180 | return (pointer->hdr.ID); |
||
| 181 | } |
||
| 182 | |||
| 183 | pointer = (RADIOBUTTON *)pointer->pNext; |
||
| 184 | } |
||
| 185 | |||
| 186 | return (-1); |
||
| 187 | } |
||
| 188 | |||
| 189 | /********************************************************************* |
||
| 190 | * Function: RbSetText(RADIOBUTTON *pRb, XCHAR *pText) |
||
| 191 | * |
||
| 192 | * Input: pRb - the pointer to the radio button |
||
| 193 | * pText - pointer to the text |
||
| 194 | * |
||
| 195 | * Output: none |
||
| 196 | * |
||
| 197 | * Overview: sets text |
||
| 198 | * |
||
| 199 | ********************************************************************/ |
||
| 200 | void RbSetText(RADIOBUTTON *pRb, XCHAR *pText) |
||
| 201 | { |
||
| 202 | pRb->pText = pText; |
||
| 203 | pRb->textHeight = GetTextHeight((BYTE *)pRb->hdr.pGolScheme->pFont); |
||
| 204 | } |
||
| 205 | |||
| 206 | /********************************************************************* |
||
| 207 | * Function: RbMsgDefault(WORD translatedMsg, RADIOBUTTON *pRb, GOL_MSG* pMsg) |
||
| 208 | * |
||
| 209 | * Overview: changes the state of the radio button by default |
||
| 210 | * |
||
| 211 | ********************************************************************/ |
||
| 212 | void RbMsgDefault(WORD translatedMsg, RADIOBUTTON *pRb, GOL_MSG *pMsg) |
||
| 213 | { |
||
| 214 | RADIOBUTTON *pointer; |
||
| 215 | |||
| 216 | #ifdef USE_FOCUS |
||
| 217 | #ifdef USE_TOUCHSCREEN |
||
| 218 | if(pMsg->type == TYPE_TOUCHSCREEN) |
||
| 219 | { |
||
| 220 | if(!GetState(pRb, RB_FOCUSED)) |
||
| 221 | { |
||
| 222 | GOLSetFocus((OBJ_HEADER *)pRb); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | #endif |
||
| 227 | #endif |
||
| 228 | if(translatedMsg == RB_MSG_CHECKED) |
||
| 229 | { |
||
| 230 | |||
| 231 | // Uncheck radio buttons in the group |
||
| 232 | pointer = (RADIOBUTTON *)pRb->pHead; |
||
| 233 | |||
| 234 | while(pointer != NULL) |
||
| 235 | { |
||
| 236 | if(GetState(pointer, RB_CHECKED)) |
||
| 237 | { |
||
| 238 | ClrState(pointer, RB_CHECKED); // reset check |
||
| 239 | SetState(pointer, RB_DRAW_CHECK); // redraw |
||
| 240 | } |
||
| 241 | |||
| 242 | pointer = (RADIOBUTTON *)pointer->pNext; |
||
| 243 | } |
||
| 244 | |||
| 245 | SetState(pRb, RB_CHECKED | RB_DRAW_CHECK); // set check and redraw |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | /********************************************************************* |
||
| 250 | * Function: WORD RbTranslateMsg(RADIOBUTTON *pRb, GOL_MSG *pMsg) |
||
| 251 | * |
||
| 252 | * Overview: translates the GOL message for the radio button |
||
| 253 | * |
||
| 254 | ********************************************************************/ |
||
| 255 | WORD RbTranslateMsg(RADIOBUTTON *pRb, GOL_MSG *pMsg) |
||
| 256 | { |
||
| 257 | |||
| 258 | // Evaluate if the message is for the radio button |
||
| 259 | // Check if disabled first |
||
| 260 | if(GetState(pRb, RB_DISABLED)) |
||
| 261 | return (OBJ_MSG_INVALID); |
||
| 262 | |||
| 263 | #ifdef USE_TOUCHSCREEN |
||
| 264 | if(pMsg->type == TYPE_TOUCHSCREEN) |
||
| 265 | { |
||
| 266 | if(pMsg->uiEvent == EVENT_PRESS) |
||
| 267 | { |
||
| 268 | |||
| 269 | // Check if it falls in the radio button borders |
||
| 270 | if |
||
| 271 | ( |
||
| 272 | (pRb->hdr.left < pMsg->param1) && |
||
| 273 | (pRb->hdr.right > pMsg->param1) && |
||
| 274 | (pRb->hdr.top < pMsg->param2) && |
||
| 275 | (pRb->hdr.bottom > pMsg->param2) |
||
| 276 | ) |
||
| 277 | { |
||
| 278 | if(!GetState(pRb, RB_CHECKED)) |
||
| 279 | return (RB_MSG_CHECKED); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | return (OBJ_MSG_INVALID); |
||
| 284 | } |
||
| 285 | |||
| 286 | #endif |
||
| 287 | #ifdef USE_KEYBOARD |
||
| 288 | if(pMsg->type == TYPE_KEYBOARD) |
||
| 289 | { |
||
| 290 | if(pMsg->param1 == pRb->hdr.ID) |
||
| 291 | { |
||
| 292 | if(pMsg->uiEvent == EVENT_KEYSCAN) |
||
| 293 | { |
||
| 294 | if((pMsg->param2 == SCAN_SPACE_PRESSED) || (pMsg->param2 == SCAN_CR_PRESSED)) |
||
| 295 | { |
||
| 296 | if(!GetState(pRb, RB_CHECKED)) |
||
| 297 | return (RB_MSG_CHECKED); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | return (OBJ_MSG_INVALID); |
||
| 303 | } |
||
| 304 | |||
| 305 | #endif |
||
| 306 | return (OBJ_MSG_INVALID); |
||
| 307 | } |
||
| 308 | |||
| 309 | /********************************************************************* |
||
| 310 | * Function: WORD RbDraw(RADIOBUTTON *pRb) |
||
| 311 | * |
||
| 312 | * Output: returns the status of the drawing |
||
| 313 | * 0 - not completed |
||
| 314 | * 1 - done |
||
| 315 | * |
||
| 316 | * Overview: draws radio button |
||
| 317 | * |
||
| 318 | ********************************************************************/ |
||
| 319 | WORD RbDraw(RADIOBUTTON *pRb) |
||
| 320 | { |
||
| 321 | typedef enum |
||
| 322 | { |
||
| 323 | REMOVE, |
||
| 324 | DRAW_BUTTON0, |
||
| 325 | DRAW_BUTTON1, |
||
| 326 | DRAW_TEXT, |
||
| 327 | DRAW_TEXT_RUN, |
||
| 328 | DRAW_CHECK, |
||
| 329 | DRAW_CHECK_RUN, |
||
| 330 | DRAW_FOC |
||
| 331 | } RB_DRAW_STATES; |
||
| 332 | |||
| 333 | #define SIN45 46341 |
||
| 334 | |||
| 335 | static RB_DRAW_STATES state = REMOVE; |
||
| 336 | SHORT checkIndent; |
||
| 337 | static SHORT radius; |
||
| 338 | static SHORT x, y; |
||
| 339 | static DWORD_VAL temp; |
||
| 340 | |||
| 341 | WORD faceClr; |
||
| 342 | |||
| 343 | if(IsDeviceBusy()) |
||
| 344 | return (0); |
||
| 345 | |||
| 346 | switch(state) |
||
| 347 | { |
||
| 348 | case REMOVE: |
||
| 349 | if(GetState(pRb, (RB_HIDE | RB_DRAW))) |
||
| 350 | { |
||
| 351 | SetColor(pRb->hdr.pGolScheme->CommonBkColor); |
||
| 352 | if(!Bar(pRb->hdr.left, pRb->hdr.top, pRb->hdr.right, pRb->hdr.bottom)) |
||
| 353 | return (0); |
||
| 354 | } |
||
| 355 | |||
| 356 | if(GetState(pRb, RB_HIDE)) |
||
| 357 | return (1); |
||
| 358 | |||
| 359 | radius = ((pRb->hdr.bottom - pRb->hdr.top) >> 1) - RB_INDENT; |
||
| 360 | x = pRb->hdr.left + ((pRb->hdr.bottom - pRb->hdr.top) >> 1) + RB_INDENT; |
||
| 361 | y = (pRb->hdr.bottom + pRb->hdr.top) >> 1; |
||
| 362 | temp.Val = SIN45; |
||
| 363 | |||
| 364 | if(GetState(pRb, RB_DRAW)) |
||
| 365 | { |
||
| 366 | state = DRAW_BUTTON0; |
||
| 367 | } |
||
| 368 | else |
||
| 369 | { |
||
| 370 | state = DRAW_CHECK; |
||
| 371 | goto rb_draw_check; |
||
| 372 | } |
||
| 373 | |||
| 374 | case DRAW_BUTTON0: |
||
| 375 | if(!GetState(pRb, RB_DISABLED)) |
||
| 376 | { |
||
| 377 | faceClr = pRb->hdr.pGolScheme->Color0; |
||
| 378 | } |
||
| 379 | else |
||
| 380 | { |
||
| 381 | faceClr = pRb->hdr.pGolScheme->ColorDisabled; |
||
| 382 | } |
||
| 383 | |||
| 384 | GOLPanelDraw |
||
| 385 | ( |
||
| 386 | x, |
||
| 387 | y, |
||
| 388 | x, |
||
| 389 | y, |
||
| 390 | radius, |
||
| 391 | faceClr, |
||
| 392 | pRb->hdr.pGolScheme->EmbossDkColor, |
||
| 393 | pRb->hdr.pGolScheme->EmbossLtColor, |
||
| 394 | NULL, |
||
| 395 | GOL_EMBOSS_SIZE |
||
| 396 | ); |
||
| 397 | state = DRAW_BUTTON1; |
||
| 398 | |||
| 399 | case DRAW_BUTTON1: |
||
| 400 | if(!GOLPanelDrawTsk()) |
||
| 401 | { |
||
| 402 | return (0); |
||
| 403 | } |
||
| 404 | |||
| 405 | state = DRAW_TEXT; |
||
| 406 | |||
| 407 | case DRAW_TEXT: |
||
| 408 | if(pRb->pText != NULL) |
||
| 409 | { |
||
| 410 | SetFont(pRb->hdr.pGolScheme->pFont); |
||
| 411 | |||
| 412 | if(GetState(pRb, RB_DISABLED)) |
||
| 413 | { |
||
| 414 | SetColor(pRb->hdr.pGolScheme->TextColorDisabled); |
||
| 415 | } |
||
| 416 | else |
||
| 417 | { |
||
| 418 | SetColor(pRb->hdr.pGolScheme->TextColor0); |
||
| 419 | } |
||
| 420 | |||
| 421 | MoveTo |
||
| 422 | ( |
||
| 423 | pRb->hdr.left + pRb->hdr.bottom - pRb->hdr.top + RB_INDENT, |
||
| 424 | (pRb->hdr.bottom + pRb->hdr.top - pRb->textHeight) >> 1 |
||
| 425 | ); |
||
| 426 | |||
| 427 | state = DRAW_TEXT_RUN; |
||
| 428 | |||
| 429 | case DRAW_TEXT_RUN: |
||
| 430 | if(!OutText(pRb->pText)) |
||
| 431 | return (0); |
||
| 432 | } |
||
| 433 | |||
| 434 | state = DRAW_CHECK; |
||
| 435 | |||
| 436 | rb_draw_check: |
||
| 437 | |||
| 438 | case DRAW_CHECK: |
||
| 439 | if(GetState(pRb, RB_CHECKED)) |
||
| 440 | { |
||
| 441 | if(GetState(pRb, RB_DISABLED)) |
||
| 442 | { |
||
| 443 | SetColor(pRb->hdr.pGolScheme->TextColorDisabled); |
||
| 444 | } |
||
| 445 | else |
||
| 446 | { |
||
| 447 | #if (COLOR_DEPTH == 1) |
||
| 448 | SetColor(BLACK); |
||
| 449 | #else |
||
| 450 | SetColor(pRb->hdr.pGolScheme->TextColor0); |
||
| 451 | #endif |
||
| 452 | } |
||
| 453 | } |
||
| 454 | else |
||
| 455 | { |
||
| 456 | if(GetState(pRb, RB_DISABLED)) |
||
| 457 | { |
||
| 458 | SetColor(pRb->hdr.pGolScheme->ColorDisabled); |
||
| 459 | } |
||
| 460 | else |
||
| 461 | { |
||
| 462 | #if (COLOR_DEPTH == 1) |
||
| 463 | SetColor(WHITE); |
||
| 464 | #else |
||
| 465 | SetColor(pRb->hdr.pGolScheme->Color0); |
||
| 466 | #endif |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | state = DRAW_CHECK_RUN; |
||
| 471 | |||
| 472 | case DRAW_CHECK_RUN: |
||
| 473 | checkIndent = (pRb->hdr.bottom - pRb->hdr.top) >> 2; |
||
| 474 | if(!FillCircle(x, y, radius - checkIndent)) |
||
| 475 | return (0); |
||
| 476 | |||
| 477 | state = DRAW_FOC; |
||
| 478 | |||
| 479 | case DRAW_FOC: |
||
| 480 | if(GetState(pRb, RB_DRAW | RB_DRAW_FOCUS)) |
||
| 481 | { |
||
| 482 | if(IsDeviceBusy()) |
||
| 483 | return (0); |
||
| 484 | |||
| 485 | if(GetState(pRb, RB_FOCUSED)) |
||
| 486 | { |
||
| 487 | SetColor(pRb->hdr.pGolScheme->TextColor0); |
||
| 488 | } |
||
| 489 | else |
||
| 490 | { |
||
| 491 | SetColor(pRb->hdr.pGolScheme->CommonBkColor); |
||
| 492 | } |
||
| 493 | |||
| 494 | SetLineType(FOCUS_LINE); |
||
| 495 | if(!Rectangle(pRb->hdr.left, pRb->hdr.top, pRb->hdr.right, pRb->hdr.bottom)) |
||
| 496 | return (0); |
||
| 497 | SetLineType(SOLID_LINE); |
||
| 498 | } |
||
| 499 | |||
| 500 | state = REMOVE; |
||
| 501 | return (1); |
||
| 502 | } |
||
| 503 | |||
| 504 | return (1); |
||
| 505 | } |
||
| 506 | |||
| 507 | #endif // USE_RADIOBUTTON |
Powered by WebSVN v2.8.3