Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/uidemo/uidemo.c

    r80d4aea rdb3895d  
    4040#include <str.h>
    4141#include <ui/entry.h>
    42 #include <ui/filedialog.h>
    4342#include <ui/fixed.h>
    4443#include <ui/image.h>
     
    4948#include <ui/msgdialog.h>
    5049#include <ui/pbutton.h>
    51 #include <ui/promptdialog.h>
    5250#include <ui/resource.h>
    5351#include <ui/ui.h>
     
    8785};
    8886
    89 static void uidemo_file_load(ui_menu_entry_t *, void *);
    9087static void uidemo_file_message(ui_menu_entry_t *, void *);
    9188static void uidemo_file_exit(ui_menu_entry_t *, void *);
    92 static void uidemo_edit_modify(ui_menu_entry_t *, void *);
    93 
    94 static void file_dialog_bok(ui_file_dialog_t *, void *, const char *);
    95 static void file_dialog_bcancel(ui_file_dialog_t *, void *);
    96 static void file_dialog_close(ui_file_dialog_t *, void *);
    97 
    98 static ui_file_dialog_cb_t file_dialog_cb = {
    99         .bok = file_dialog_bok,
    100         .bcancel = file_dialog_bcancel,
    101         .close = file_dialog_close
    102 };
    103 
    104 static void prompt_dialog_bok(ui_prompt_dialog_t *, void *, const char *);
    105 static void prompt_dialog_bcancel(ui_prompt_dialog_t *, void *);
    106 static void prompt_dialog_close(ui_prompt_dialog_t *, void *);
    107 
    108 static ui_prompt_dialog_cb_t prompt_dialog_cb = {
    109         .bok = prompt_dialog_bok,
    110         .bcancel = prompt_dialog_bcancel,
    111         .close = prompt_dialog_close
    112 };
    11389
    11490static void msg_dialog_button(ui_msg_dialog_t *, void *, unsigned);
     
    12096};
    12197
    122 /** Horizontal alignment selected by each radio button */
    123 static const gfx_halign_t uidemo_halign[3] = {
    124         gfx_halign_left,
    125         gfx_halign_center,
    126         gfx_halign_right
    127 };
    128 
    12998/** Window close button was clicked.
    13099 *
     
    153122                if (rc != EOK)
    154123                        printf("Error changing entry text.\n");
     124                (void) ui_entry_paint(demo->entry);
    155125        } else {
    156126                rc = ui_entry_set_text(demo->entry, "Cancel pressed");
    157127                if (rc != EOK)
    158128                        printf("Error changing entry text.\n");
     129                (void) ui_entry_paint(demo->entry);
    159130        }
    160131}
     
    168139{
    169140        ui_demo_t *demo = (ui_demo_t *) arg;
    170 
    171         ui_entry_set_read_only(demo->entry, enable);
     141        errno_t rc;
     142
     143        if (enable) {
     144                rc = ui_entry_set_text(demo->entry, "Checked");
     145                if (rc != EOK)
     146                        printf("Error changing entry text.\n");
     147                (void) ui_entry_paint(demo->entry);
     148        } else {
     149                rc = ui_entry_set_text(demo->entry, "Unchecked");
     150                if (rc != EOK)
     151                        printf("Error changing entry text.\n");
     152                (void) ui_entry_paint(demo->entry);
     153        }
    172154}
    173155
     
    181163{
    182164        ui_demo_t *demo = (ui_demo_t *) garg;
    183         gfx_halign_t halign = *(gfx_halign_t *) barg;
    184 
    185         ui_entry_set_halign(demo->entry, halign);
     165        const char *text = (const char *) barg;
     166        errno_t rc;
     167
     168        rc = ui_entry_set_text(demo->entry, text);
     169        if (rc != EOK)
     170                printf("Error changing entry text.\n");
    186171        (void) ui_entry_paint(demo->entry);
    187172}
     
    215200}
    216201
    217 /** Display a message window.
    218  *
    219  * @param demo UI demo
    220  * @param caption Window caption
    221  * @param text Message text
    222  */
    223 static void uidemo_show_message(ui_demo_t *demo, const char *caption,
    224     const char *text)
    225 {
    226         ui_msg_dialog_params_t mdparams;
    227         ui_msg_dialog_t *dialog;
    228         errno_t rc;
    229 
    230         ui_msg_dialog_params_init(&mdparams);
    231         mdparams.caption = caption;
    232         mdparams.text = text;
    233 
    234         rc = ui_msg_dialog_create(demo->ui, &mdparams, &dialog);
    235         if (rc != EOK) {
    236                 printf("Error creating message dialog.\n");
    237                 return;
    238         }
    239 
    240         ui_msg_dialog_set_cb(dialog, &msg_dialog_cb, &demo);
    241 }
    242 
    243 /** File / Load menu entry selected.
    244  *
    245  * @param mentry Menu entry
    246  * @param arg Argument (demo)
    247  */
    248 static void uidemo_file_load(ui_menu_entry_t *mentry, void *arg)
    249 {
    250         ui_demo_t *demo = (ui_demo_t *) arg;
    251         ui_file_dialog_params_t fdparams;
    252         ui_file_dialog_t *dialog;
    253         errno_t rc;
    254 
    255         ui_file_dialog_params_init(&fdparams);
    256         fdparams.caption = "Load File";
    257 
    258         rc = ui_file_dialog_create(demo->ui, &fdparams, &dialog);
    259         if (rc != EOK) {
    260                 printf("Error creating message dialog.\n");
    261                 return;
    262         }
    263 
    264         ui_file_dialog_set_cb(dialog, &file_dialog_cb, demo);
    265 }
    266 
    267 /** File / Message menu entry selected.
     202/** File/message menu entry selected.
    268203 *
    269204 * @param mentry Menu entry
     
    288223
    289224        ui_msg_dialog_set_cb(dialog, &msg_dialog_cb, &demo);
    290 }
    291 
    292 /** File / Exit menu entry selected.
     225
     226}
     227
     228/** File/exit menu entry selected.
    293229 *
    294230 * @param mentry Menu entry
     
    300236
    301237        ui_quit(demo->ui);
    302 }
    303 
    304 /** Edit / Modify menu entry selected.
    305  *
    306  * @param mentry Menu entry
    307  * @param arg Argument (demo)
    308  */
    309 static void uidemo_edit_modify(ui_menu_entry_t *mentry, void *arg)
    310 {
    311         ui_demo_t *demo = (ui_demo_t *) arg;
    312         ui_prompt_dialog_params_t pdparams;
    313         ui_prompt_dialog_t *dialog;
    314         errno_t rc;
    315 
    316         ui_prompt_dialog_params_init(&pdparams);
    317         pdparams.caption = "Modify Entry Text";
    318         pdparams.prompt = "Enter New Text";
    319 
    320         rc = ui_prompt_dialog_create(demo->ui, &pdparams, &dialog);
    321         if (rc != EOK) {
    322                 printf("Error creating message dialog.\n");
    323                 return;
    324         }
    325 
    326         ui_prompt_dialog_set_cb(dialog, &prompt_dialog_cb, demo);
    327 }
    328 
    329 /** File dialog OK button press.
    330  *
    331  * @param dialog File dialog
    332  * @param arg Argument (ui_demo_t *)
    333  * @param fname File name
    334  */
    335 static void file_dialog_bok(ui_file_dialog_t *dialog, void *arg,
    336     const char *fname)
    337 {
    338         ui_demo_t *demo = (ui_demo_t *) arg;
    339         char buf[128];
    340         char *p;
    341         FILE *f;
    342 
    343         ui_file_dialog_destroy(dialog);
    344 
    345         f = fopen(fname, "rt");
    346         if (f == NULL) {
    347                 uidemo_show_message(demo, "Error", "Error opening file.");
    348                 return;
    349         }
    350 
    351         p = fgets(buf, sizeof(buf), f);
    352         if (p == NULL) {
    353                 uidemo_show_message(demo, "Error", "Error reading file.");
    354                 fclose(f);
    355                 return;
    356         }
    357 
    358         /* Cut string off at the first non-printable character */
    359         p = buf;
    360         while (*p != '\0') {
    361                 if (*p < ' ') {
    362                         *p = '\0';
    363                         break;
    364                 }
    365                 ++p;
    366         }
    367 
    368         ui_entry_set_text(demo->entry, buf);
    369         fclose(f);
    370 }
    371 
    372 /** File dialog cancel button press.
    373  *
    374  * @param dialog File dialog
    375  * @param arg Argument (ui_demo_t *)
    376  */
    377 static void file_dialog_bcancel(ui_file_dialog_t *dialog, void *arg)
    378 {
    379         ui_demo_t *demo = (ui_demo_t *) arg;
    380 
    381         (void) demo;
    382         ui_file_dialog_destroy(dialog);
    383 }
    384 
    385 /** File dialog close request.
    386  *
    387  * @param dialog File dialog
    388  * @param arg Argument (ui_demo_t *)
    389  */
    390 static void file_dialog_close(ui_file_dialog_t *dialog, void *arg)
    391 {
    392         ui_demo_t *demo = (ui_demo_t *) arg;
    393 
    394         (void) demo;
    395         ui_file_dialog_destroy(dialog);
    396 }
    397 
    398 /** Prompt dialog OK button press.
    399  *
    400  * @param dialog Prompt dialog
    401  * @param arg Argument (ui_demo_t *)
    402  * @param text Submitted text
    403  */
    404 static void prompt_dialog_bok(ui_prompt_dialog_t *dialog, void *arg,
    405     const char *text)
    406 {
    407         ui_demo_t *demo = (ui_demo_t *) arg;
    408 
    409         ui_prompt_dialog_destroy(dialog);
    410         ui_entry_set_text(demo->entry, text);
    411 }
    412 
    413 /** Prompt dialog cancel button press.
    414  *
    415  * @param dialog File dialog
    416  * @param arg Argument (ui_demo_t *)
    417  */
    418 static void prompt_dialog_bcancel(ui_prompt_dialog_t *dialog, void *arg)
    419 {
    420         ui_demo_t *demo = (ui_demo_t *) arg;
    421 
    422         (void) demo;
    423         ui_prompt_dialog_destroy(dialog);
    424 }
    425 
    426 /** Prompt dialog close request.
    427  *
    428  * @param dialog File dialog
    429  * @param arg Argument (ui_demo_t *)
    430  */
    431 static void prompt_dialog_close(ui_prompt_dialog_t *dialog, void *arg)
    432 {
    433         ui_demo_t *demo = (ui_demo_t *) arg;
    434 
    435         (void) demo;
    436         ui_prompt_dialog_destroy(dialog);
    437238}
    438239
     
    479280        gfx_coord2_t off;
    480281        ui_menu_entry_t *mmsg;
    481         ui_menu_entry_t *mload;
    482282        ui_menu_entry_t *mfoo;
    483283        ui_menu_entry_t *mbar;
    484284        ui_menu_entry_t *mfoobar;
    485         ui_menu_entry_t *msep;
    486285        ui_menu_entry_t *mexit;
    487         ui_menu_entry_t *mmodify;
    488286        ui_menu_entry_t *mabout;
    489287        errno_t rc;
     
    506304                params.rect.p0.x = 0;
    507305                params.rect.p0.y = 0;
    508                 params.rect.p1.x = 44;
    509                 params.rect.p1.y = 21;
     306                params.rect.p1.x = 80;
     307                params.rect.p1.y = 25;
    510308        } else {
    511309                params.rect.p0.x = 0;
     
    553351        ui_menu_entry_set_cb(mmsg, uidemo_file_message, (void *) &demo);
    554352
    555         rc = ui_menu_entry_create(demo.mfile, "Load", "", &mload);
    556         if (rc != EOK) {
    557                 printf("Error creating menu.\n");
    558                 return rc;
    559         }
    560 
    561         ui_menu_entry_set_cb(mload, uidemo_file_load, (void *) &demo);
    562 
    563353        rc = ui_menu_entry_create(demo.mfile, "Foo", "Ctrl-Alt-Del", &mfoo);
    564354        if (rc != EOK) {
     
    579369        }
    580370
    581         rc = ui_menu_entry_sep_create(demo.mfile, &msep);
     371        rc = ui_menu_entry_sep_create(demo.mfile, &mexit);
    582372        if (rc != EOK) {
    583373                printf("Error creating menu.\n");
     
    598388                return rc;
    599389        }
    600 
    601         rc = ui_menu_entry_create(demo.medit, "Modify", "", &mmodify);
    602         if (rc != EOK) {
    603                 printf("Error creating menu.\n");
    604                 return rc;
    605         }
    606 
    607         ui_menu_entry_set_cb(mmodify, uidemo_edit_modify, (void *) &demo);
    608390
    609391        rc = ui_menu_create(demo.mbar, "Preferences", &demo.mpreferences);
     
    628410        if (ui_is_textmode(ui)) {
    629411                rect.p0.x = 1;
    630                 rect.p0.y = 1;
    631                 rect.p1.x = 43;
    632                 rect.p1.y = 2;
     412                rect.p0.y = 2;
     413                rect.p1.x = 79;
     414                rect.p1.y = 3;
    633415        } else {
    634416                rect.p0.x = 4;
     
    651433        }
    652434
    653         /* FIXME: Auto layout */
    654         if (ui_is_textmode(ui)) {
    655                 rect.p0.x = 2;
    656                 rect.p0.y = 3;
    657                 rect.p1.x = 42;
    658                 rect.p1.y = 4;
    659         } else {
    660                 rect.p0.x = 15;
    661                 rect.p0.y = 53;
    662                 rect.p1.x = 205;
    663                 rect.p1.y = 78;
    664         }
    665 
     435        rect.p0.x = 15;
     436        rect.p0.y = 53;
     437        rect.p1.x = 205;
     438        rect.p1.y = 78;
    666439        ui_entry_set_rect(demo.entry, &rect);
    667440        ui_entry_set_halign(demo.entry, gfx_halign_center);
     
    679452        }
    680453
    681         /* FIXME: Auto layout */
    682         if (ui_is_textmode(ui)) {
    683                 rect.p0.x = 2;
    684                 rect.p0.y = 5;
    685                 rect.p1.x = 42;
    686                 rect.p1.y = 6;
    687         } else {
    688                 rect.p0.x = 60;
    689                 rect.p0.y = 88;
    690                 rect.p1.x = 160;
    691                 rect.p1.y = 101;
    692         }
    693 
     454        rect.p0.x = 60;
     455        rect.p0.y = 88;
     456        rect.p1.x = 160;
     457        rect.p1.y = 101;
    694458        ui_label_set_rect(demo.label, &rect);
    695459        ui_label_set_halign(demo.label, gfx_halign_center);
     
    709473        ui_pbutton_set_cb(demo.pb1, &pbutton_cb, (void *) &demo);
    710474
    711         /* FIXME: Auto layout */
    712         if (ui_is_textmode(ui)) {
    713                 rect.p0.x = 2;
    714                 rect.p0.y = 7;
    715                 rect.p1.x = 12;
    716                 rect.p1.y = 8;
    717         } else {
    718                 rect.p0.x = 15;
    719                 rect.p0.y = 111;
    720                 rect.p1.x = 105;
    721                 rect.p1.y = 139;
    722         }
    723 
     475        rect.p0.x = 15;
     476        rect.p0.y = 111;
     477        rect.p1.x = 105;
     478        rect.p1.y = 139;
    724479        ui_pbutton_set_rect(demo.pb1, &rect);
    725480
     
    740495        ui_pbutton_set_cb(demo.pb2, &pbutton_cb, (void *) &demo);
    741496
    742         if (ui_is_textmode(ui)) {
    743                 rect.p0.x = 32;
    744                 rect.p0.y = 7;
    745                 rect.p1.x = 42;
    746                 rect.p1.y = 8;
    747         } else {
    748                 rect.p0.x = 115;
    749                 rect.p0.y = 111;
    750                 rect.p1.x = 205;
    751                 rect.p1.y = 139;
    752         }
    753 
     497        rect.p0.x = 115;
     498        rect.p0.y = 111;
     499        rect.p1.x = 205;
     500        rect.p1.y = 139;
    754501        ui_pbutton_set_rect(demo.pb2, &rect);
    755502
     
    761508
    762509        gfx_bitmap_params_init(&bparams);
    763         if (ui_is_textmode(ui)) {
    764                 bparams.rect.p0.x = 0;
    765                 bparams.rect.p0.y = 0;
    766                 bparams.rect.p1.x = 40;
    767                 bparams.rect.p1.y = 2;
    768         } else {
    769                 bparams.rect.p0.x = 0;
    770                 bparams.rect.p0.y = 0;
    771                 bparams.rect.p1.x = 188;
    772                 bparams.rect.p1.y = 24;
    773         }
     510        bparams.rect.p0.x = 0;
     511        bparams.rect.p0.y = 0;
     512        bparams.rect.p1.x = 188;
     513        bparams.rect.p1.y = 24;
    774514
    775515        rc = gfx_bitmap_create(gc, &bparams, NULL, &bitmap);
     
    787527        }
    788528
    789         if (ui_is_textmode(ui)) {
    790                 off.x = 2;
    791                 off.y = 9;
    792         } else {
    793                 off.x = 15;
    794                 off.y = 155;
    795         }
    796 
     529        off.x = 15;
     530        off.y = 155;
    797531        gfx_rect_translate(&off, &bparams.rect, &rect);
    798532
    799533        /* Adjust for frame width (2 x 1 pixel) */
    800         if (!ui_is_textmode(ui)) {
    801                 ui_image_set_flags(demo.image, ui_imgf_frame);
    802                 rect.p1.x += 2;
    803                 rect.p1.y += 2;
    804         }
    805 
     534        rect.p1.x += 2;
     535        rect.p1.y += 2;
    806536        ui_image_set_rect(demo.image, &rect);
     537        ui_image_set_flags(demo.image, ui_imgf_frame);
    807538
    808539        rc = ui_fixed_add(demo.fixed, ui_image_ctl(demo.image));
     
    812543        }
    813544
    814         rc = ui_checkbox_create(ui_res, "Read only", &demo.checkbox);
     545        rc = ui_checkbox_create(ui_res, "Check me", &demo.checkbox);
    815546        if (rc != EOK) {
    816547                printf("Error creating check box.\n");
     
    820551        ui_checkbox_set_cb(demo.checkbox, &checkbox_cb, (void *) &demo);
    821552
    822         /* FIXME: Auto layout */
    823         if (ui_is_textmode(ui)) {
    824                 rect.p0.x = 2;
    825                 rect.p0.y = 12;
    826                 rect.p1.x = 12;
    827                 rect.p1.y = 13;
    828         } else {
    829                 rect.p0.x = 15;
    830                 rect.p0.y = 190;
    831                 rect.p1.x = 140;
    832                 rect.p1.y = 210;
    833         }
    834 
     553        rect.p0.x = 15;
     554        rect.p0.y = 190;
     555        rect.p1.x = 140;
     556        rect.p1.y = 210;
    835557        ui_checkbox_set_rect(demo.checkbox, &rect);
    836558
     
    847569        }
    848570
    849         rc = ui_rbutton_create(demo.rbgroup, "Left", (void *) &uidemo_halign[0],
    850             &demo.rbleft);
     571        rc = ui_rbutton_create(demo.rbgroup, "Option 1", (void *) "First",
     572            &demo.rb1);
    851573        if (rc != EOK) {
    852574                printf("Error creating radio button.\n");
     
    857579            (void *) &demo);
    858580
    859         /* FIXME: Auto layout */
    860         if (ui_is_textmode(ui)) {
    861                 rect.p0.x = 2;
    862                 rect.p0.y = 14;
    863                 rect.p1.x = 12;
    864                 rect.p1.y = 15;
    865         } else {
    866                 rect.p0.x = 15;
    867                 rect.p0.y = 220;
    868                 rect.p1.x = 140;
    869                 rect.p1.y = 240;
    870         }
    871         ui_rbutton_set_rect(demo.rbleft, &rect);
    872 
    873         rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbleft));
    874         if (rc != EOK) {
    875                 printf("Error adding control to layout.\n");
    876                 return rc;
    877         }
    878 
    879         rc = ui_rbutton_create(demo.rbgroup, "Center", (void *) &uidemo_halign[1],
    880             &demo.rbcenter);
     581        rect.p0.x = 15;
     582        rect.p0.y = 220;
     583        rect.p1.x = 140;
     584        rect.p1.y = 240;
     585        ui_rbutton_set_rect(demo.rb1, &rect);
     586
     587        rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rb1));
     588        if (rc != EOK) {
     589                printf("Error adding control to layout.\n");
     590                return rc;
     591        }
     592
     593        rc = ui_rbutton_create(demo.rbgroup, "Option 2", (void *) "Second",
     594            &demo.rb2);
    881595        if (rc != EOK) {
    882596                printf("Error creating radio button.\n");
     
    884598        }
    885599
    886         /* FIXME: Auto layout */
    887         if (ui_is_textmode(ui)) {
    888                 rect.p0.x = 2;
    889                 rect.p0.y = 15;
    890                 rect.p1.x = 12;
    891                 rect.p1.y = 16;
    892         } else {
    893                 rect.p0.x = 15;
    894                 rect.p0.y = 250;
    895                 rect.p1.x = 140;
    896                 rect.p1.y = 270;
    897         }
    898         ui_rbutton_set_rect(demo.rbcenter, &rect);
    899         ui_rbutton_select(demo.rbcenter);
    900 
    901         rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbcenter));
    902         if (rc != EOK) {
    903                 printf("Error adding control to layout.\n");
    904                 return rc;
    905         }
    906 
    907         rc = ui_rbutton_create(demo.rbgroup, "Right", (void *) &uidemo_halign[2],
    908             &demo.rbright);
     600        rect.p0.x = 15;
     601        rect.p0.y = 250;
     602        rect.p1.x = 140;
     603        rect.p1.y = 270;
     604        ui_rbutton_set_rect(demo.rb2, &rect);
     605
     606        rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rb2));
     607        if (rc != EOK) {
     608                printf("Error adding control to layout.\n");
     609                return rc;
     610        }
     611
     612        rc = ui_rbutton_create(demo.rbgroup, "Option 3", (void *) "Third",
     613            &demo.rb3);
    909614        if (rc != EOK) {
    910615                printf("Error creating radio button.\n");
     
    912617        }
    913618
    914         /* FIXME: Auto layout */
    915         if (ui_is_textmode(ui)) {
    916                 rect.p0.x = 2;
    917                 rect.p0.y = 16;
    918                 rect.p1.x = 12;
    919                 rect.p1.y = 17;
    920         } else {
    921                 rect.p0.x = 15;
    922                 rect.p0.y = 280;
    923                 rect.p1.x = 140;
    924                 rect.p1.y = 300;
    925         }
    926         ui_rbutton_set_rect(demo.rbright, &rect);
    927 
    928         rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rbright));
     619        rect.p0.x = 15;
     620        rect.p0.y = 280;
     621        rect.p1.x = 140;
     622        rect.p1.y = 300;
     623        ui_rbutton_set_rect(demo.rb3, &rect);
     624
     625        rc = ui_fixed_add(demo.fixed, ui_rbutton_ctl(demo.rb3));
    929626        if (rc != EOK) {
    930627                printf("Error adding control to layout.\n");
     
    940637        ui_slider_set_cb(demo.slider, &slider_cb, (void *) &demo);
    941638
    942         /* FIXME: Auto layout */
    943         if (ui_is_textmode(ui)) {
    944                 rect.p0.x = 2;
    945                 rect.p0.y = 18;
    946                 rect.p1.x = 12;
    947                 rect.p1.y = 19;
    948         } else {
    949                 rect.p0.x = 15;
    950                 rect.p0.y = 310;
    951                 rect.p1.x = 130;
    952                 rect.p1.y = 330;
    953         }
    954 
     639        rect.p0.x = 15;
     640        rect.p0.y = 310;
     641        rect.p1.x = 130;
     642        rect.p1.y = 330;
    955643        ui_slider_set_rect(demo.slider, &rect);
    956644
     
    1005693                        k = i * i + j * j;
    1006694                        pixelmap_put_pixel(&pixelmap, i, j,
    1007                             PIXEL(0, k, k, 255 - k));
     695                            PIXEL(255, k, k, 255 - k));
    1008696                }
    1009697        }
Note: See TracChangeset for help on using the changeset viewer.