home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / tut-code.lha / tut-code / menu1 / main.c < prev   
Encoding:
C/C++ Source or Header  |  1992-01-03  |  3.2 KB  |  150 lines

  1. #include <IV-look/kit.h>
  2. #include <IV-look/menu.h>
  3. #include <InterViews/action.h>
  4. #include <InterViews/background.h>
  5. #include <InterViews/box.h>
  6. #include <InterViews/place.h>
  7. #include <InterViews/session.h>
  8. #include <InterViews/style.h>
  9. #include <InterViews/window.h>
  10. #include <stdio.h>
  11.  
  12. struct CmdInfo;
  13.  
  14. class App {
  15. public:
  16.     void open(), save(), quit();
  17.  
  18.     void cut();
  19.     void copy();
  20.     void paste();
  21.  
  22.     void black();
  23.     void red();
  24.     void green();
  25.     void blue();
  26.  
  27.     Menu* menubar(CmdInfo*, Kit*, Style*);
  28. private:
  29.     Menu* pulldown(CmdInfo*, int, Kit*, Style*);
  30. };
  31.  
  32. declare(ActionCallback,App);
  33. implement(ActionCallback,App);
  34.  
  35. struct CmdInfo {
  36.     const char* str;
  37.     ActionMemberFunction(App)* func;
  38.     CmdInfo* submenu;
  39.     int options;
  40. };
  41.  
  42. CmdInfo filemenu[] = {
  43.     { "Open", &App::open },
  44.     { "Save", &App::save },
  45.     { "", nil },
  46.     { "Quit", &App::quit },
  47.     { nil }
  48. };
  49.  
  50. CmdInfo editmenu[] = {
  51.     { "Cut", &App::cut },
  52.     { "Copy", &App::copy },
  53.     { "Paste", &App::paste },
  54.     { nil }
  55. };
  56.  
  57. CmdInfo colormenu[] = {
  58.     { "Black", &App::black },
  59.     { "Red", &App::red },
  60.     { "Green", &App::green },
  61.     { "Blue", &App::blue },
  62.     { nil }
  63. };
  64.  
  65. CmdInfo bar[] = {
  66.     { "File", nil, filemenu, 0 },
  67.     { "Edit", nil, editmenu, 1 },
  68.     { "Color", nil, colormenu, 2 },
  69.     { nil }
  70. };
  71.  
  72. void App::open() { printf("open\n"); }
  73. void App::save() { printf("save\n"); }
  74. void App::quit() { Session::instance()->quit(); }
  75.  
  76. void App::cut() { printf("cut\n"); }
  77. void App::copy() { printf("copy\n"); }
  78. void App::paste() { printf("paste\n"); }
  79.  
  80. void App::black() { printf("black\n"); }
  81. void App::red() { printf("red\n"); }
  82. void App::green() { printf("green\n"); }
  83. void App::blue() { printf("blue\n"); }
  84.  
  85. Menu* App::menubar(CmdInfo* info, Kit* kit, Style* style) {
  86.     Menu* m = kit->menubar(style);
  87.     for (CmdInfo* i = info; i->str != nil; i++) {
  88.     m->add_item(
  89.         kit->menubar_item(
  90.         kit->fancy_label(i->str, style),
  91.         style
  92.         ),
  93.         pulldown(i->submenu, i->options, kit, style)
  94.     );
  95.     }
  96.     m->menu(0)->telltale(1)->set(Telltale::is_enabled, false);
  97.  
  98.     return m;
  99. }
  100.  
  101. Menu* App::pulldown(CmdInfo* info, int opt, Kit* k, Style* s) {
  102.     Menu* m = k->pulldown(s);
  103.     TelltaleGroup* group = nil;
  104.     for (CmdInfo* i = info; i->str != nil; i++) {
  105.         if (i->str[0] == '\0') {
  106.             m->add_item(k->menu_item_separator(s));
  107.         } else {
  108.         Glyph* g = new RMargin(
  109.         k->fancy_label(i->str, s), 0.0, fil, 0.0
  110.         );
  111.         Telltale* t;
  112.         switch (opt) {
  113.         case 1:
  114.         t = k->toggle_menu_item(g, s);
  115.         break;
  116.         case 2:
  117.         if (group == nil) {
  118.             group = new TelltaleGroup;
  119.         }
  120.         t = k->radio_menu_item(group, g, s);
  121.         break;
  122.         default:
  123.         t = k->menu_item(g, s);
  124.         break;
  125.         }
  126.         if (i->func == nil && i->submenu != nil) {
  127.         m->add_item(t, pulldown(i->submenu, i->options, k, s));
  128.         } else {
  129.         m->add_item(t, new ActionCallback(App)(this, i->func));
  130.         }
  131.         }
  132.     }
  133.     return m;
  134. }
  135.  
  136. int main(int argc, char** argv) {
  137.     Session* session = new Session("Himom", argc, argv);
  138.     Style* style = session->style();
  139.     Kit* kit = Kit::instance();
  140.     App* a = new App;
  141.     return session->run_window(
  142.     new ApplicationWindow(
  143.         new Background(
  144.         new Margin(a->menubar(bar, kit, style), 10.0),
  145.         style->flat()
  146.         )
  147.     )
  148.     );
  149. }
  150.