File: win-demo.c

package info (click to toggle)
grafix 1.6-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,156 kB
  • ctags: 1,962
  • sloc: ansic: 20,183; makefile: 186; sh: 3
file content (165 lines) | stat: -rw-r--r-- 5,474 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// win-demo.c : 
//     demonstration of some basic classes and functionality of grafix

#include "window.h"

// derived class of window
class my_window : public window {
public: my_window(window & parent, int w, int h, int x, int y) :
  window(parent,w,h,x,y) {}
  virtual void redraw() { // virtual fn replaces window::redraw
    int y; 
    for (y=0; y < height ; y+= 3) line(0, y, width, y); 
  }
};


// window shows a variable string which is selected from radio_menu 
// deft is the starting value
class displ : public window {
public:
    char * val;
    displ(window & parent, char * deft, int w, int h, int x, int y) :
    window (parent,w,h,x,y) { val = deft; } 

    virtual void redraw()  { clear(); PlaceText(val); }
    virtual void action(char* /* menu */, char * value) { 
      val = value;
      redraw(); 
    }
};

// test of  keyboard-Events : output of state and key
class key_test : public button { 
char info[200]; 
public:
  key_test(window & parent, int w, int h, int x, int y) :
    button (parent,"keyboard test",w,h,x,y) {
    selection_mask |= KeyPressMask;
    add_help("Press any key","to show its","State, Code, Keysym",0);
  }
  virtual void KeyPress_CB(XKeyEvent ev) { 
    // only Shift state (0x1) is recongnized in  XLookupKeysym
    KeySym keysym = XLookupKeysym(&ev,ev.state & 1);
    sprintf(info,"state = 0x%x code = 0x%x keysym = 0x%lx -> '%s'",
	    ev.state,ev.keycode,keysym,XKeysymToString(keysym));
    printf("%s\n",info); 
    Name = info; clear(); // draw info for Name
    redraw();
  }
};

// quit as a callback function
void cb_fn() { printf(" cb   \n"); }
void quit() { exit(0); }
void funct(int n, char * s1, char * s2) {
  printf("funct called with int %d, char * %s, char * %s\n",n,s1,s2); 
}

scrollbar * scbr;
main_window *mainw;

void sbinf() { printf(" %g",scbr->value); fflush(stdout); };

// lists all children recursively
void child_tree(window *pp) {
  static int nest = 0;
  win_list * cc = pp->children;
  nest++;
  while (cc) { 
    window * cw = cc->child;
    int i; for (i=0; i < nest; i++) printf("  "); // recursion depth
    printf("Win %lx (%dx%d) at %d,%d\n",cw->Win,cw->width,cw->height,
	   cc->x,cc->y);
    child_tree(cw);
    cc = cc->next; }
  nest--;
}

void child_print(window *pp) {
  printf("window tree of Win %lx (%dx%d)\n",pp->Win,pp->width,pp->height);
  child_tree(pp);
} 

static Bool toggle = False; // state of sb-button : toggle it
void disable(button *sb) { if (toggle) sb->disable(); else sb->enable(); }

int main(int /* argc */, char *argv[]) { 
  main_window *mainw = new main_window(argv[0], 300, 300);  // Main Window

  // 1. child : a quit button
  window* qb = new quit_button(*mainw,100,20, 0, 0);
  qb->add_help("this is","dynamic help","for quit",0); 
  qb->CB_info = TRUE; // debug inform about all event

  // 2. child : simple window  
  window *child = new window(*mainw,  80, 100, 0, 100); 

  new button(*child, "button", 50, 20, 10, 10);
  // a button with callback-function 
  button *sb = new callback_button(*child, "cb-info", cb_fn, 50,20,10, 40); 
  sb->CB_info = TRUE; // debug inform about all events
  // toggle state of sb : 
  new switch_button(*child, "disable", "enable", &toggle, (VVP) &disable, sb, 50,20,10, 70);
  
  // a popup window connected with a popup-button  
  main_window *popup = new main_window("popup", 200, 100); 
  popup->CB_info = True;
  new button(*popup, "hello", 100, 20, 0, 0);
  new unmap_button(*popup, "delete",100, 20, 100, 0);
  new popup_button(*mainw, popup, "popup", 100, 20, 0, 30);

  // help button 
  char * text[] = {"this is help text for help button", "here the 2. line", 0};
  new help_button(*mainw, 20, 60, text);
  new my_window(*mainw, 60, 60, 110, 0); // user-defined window 

  // a simple pulldown window connected with pulldown_button 
  // and two childs
  pulldown_window * pulldown;
  int w = 80, h = 20, x = 210, y = 0; 
  pulldown = new pulldown_window (w, 200, 0, 0);

  new button (*pulldown,"pp",w, 20, 0, 30);
  new pulldown_button(*mainw, pulldown, "pulldown-1", w, h, x, y);
  y+= 30;

  struct but_cb pd_list[] = { {"button1", cb_fn}, {"quit", quit}};
  make_pulldown_menu(*mainw, "pulldown-2", 2, pd_list, w, h, x, y); y+= 30;

  // xwd_button : calls "xwd -id wid | xwud &" (produces copy on  screen)
  xwd_button xwd(*mainw,"dump"," | xwud &", mainw, w, h, x, y); 
  y+= 30;
  
  //  new template_button <void*>(*mainw,"children", 
  //			      &child_print, (void*) mainw, w, h, x, y);
  y+= 30;

  // radio button menu with display of toggled value
  window *radio = new window(*mainw,100,70,100,100); 
  displ *dsp = new displ(*radio,"display",80,20,10,10);
  char *blist[] = { "val-1", "***b2***", "val-2" , "xxxxxx", 0 };
  char *help[] = {"this Help Text ","appears", "upon enter", "as popup",0};
  make_radio_menu (*radio, "radio", blist, help, dsp, 80, 20, 10, 30); 

  new key_test(*mainw,300,20,0,200);
  
  scbr = new scrollbar (*mainw,&sbinf,200,30,0,240,0,1000,500,"x = %.0f");
  
  // a menu bar with autoplace buttons
  menu_bar *mb = new menu_bar(*mainw,300,25,0,270);
  int value = 1; // a toggle value for toggle_button below
  new button(*mb,"buttons"); new button(*mb, "with");
  new button(*mb,"autosizing");
  // Demo for function_button -> calls 'funct'
  new function_button(*mb,"funct",(CB) funct,1,"2.arg","3.");
  new toggle_button(*mb, "toggle", &value);
  new quit_button(*mb);

  mainw->main_loop();
  // delete mainw; -> is done automatically !
}