File: callbacks.c

package info (click to toggle)
libsx 2.08-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 2,668 kB
  • sloc: ansic: 8,739; sh: 8,603; makefile: 62
file content (424 lines) | stat: -rw-r--r-- 9,381 bytes parent folder | download | duplicates (4)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* This file contains routines that are called when a button is pressed 
 * in your window or when things happen in a drawing area.
 *
 * If you add a function to this file, you should also add a function 
 * prototype for it to the callbacks.h file (unless it is an internal 
 * function, then you should just add it down below where it says 
 * "internal prototypes").
 *
 *              --  This code is under the GNU copyleft  --
 *
 *   Dominic Giampaolo
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "libsx.h"
#include "main.h"
#include "callbacks.h"


static int width, height;



/*  -- CALLBACK ROUTINES  --
 *
 * These functions are called when various things happen in your windows.
 * They should handle what happened and give feedback to the user
 */


void quit(Widget w, void *data)
{
  MyProgram *me = (MyProgram *)data;
  int index;
  char *str;

  str = GetStringEntry(me->str_entry);
  printf("Final value for string entry is: %s\n", str);

  index = GetCurrentListItem(me->list);
  printf("Current list index is: %d\n", index);

  CloseWindow();   /* This will free any resources and exit MainLoop() */
}



void load(Widget w1, void *data)
{
  char *fname;

  fname = GetString("Enter new window title", "X Blows");
  if (fname)
   {
     printf("You entered: %s\n", fname);
     SetWindowTitle(fname);
     free(fname);
   }
  else
    printf("You clicked cancel.\n");
}


void save(Widget w, void *data)
{
  int ans;

  /* ans = GetYesNo("\nAre you a weenie?\n\n"); */
  ans = GetTriState("\nAre you a weenie?\n\n");

  if (ans == TRUE)
     printf("You're a weenie.\n");
  else if (ans == FALSE)
    printf("You are not a weenie.\n");
  else if (ans == -1)
    printf("You canceled.\n");
}


void scroll_func(Widget w, float val, void *arg)
{
  printf("new value is: %f\n", val);
}


void string_func(Widget w, char *txt, void *arg)
{
  MyProgram *me = (MyProgram *)arg;
  FILE *fp;

  printf("Got text: %s\n", txt);
  
  if ((fp = fopen(txt, "r")) != NULL)  /* test if it exists first */
   {
     fclose(fp);

     SetTextWidgetText(me->text_widget, txt, EDIT_FILE);
     SetTextWidgetPosition(me->text_widget, 3000);
   }
  else
   {
     SetTextWidgetText(me->text_widget, txt, EDIT_STRING);
   }
  
}



void list_callback(Widget w, char *str, int index, void *arg)
{
  printf("In list callback, got item: %s  index==%d\n", str, index);
}


void do_stuff(Widget w, void *data)
{
  int i;
  static int counter=2, asked=0;
  MyProgram *me=data;
  char *str="This takes over the colormap of the display.\n\n Are you Sure?";

  if (asked == FALSE && GetYesNo(str) == FALSE)
    return;
  asked=1;

  GetAllColors();
  SetColorMap(counter);
  counter = (counter + 1) % 4;

  SetFgColor(me->quit,     248);   /* 255 should always be an ok color */
  SetBorderColor(me->quit, 248);

  SetFgColor(me->color_widget,     248);
  SetBorderColor(me->color_widget, 248);

  me->in_color_mode = 1;
  for(i=0; i < 256; i++)
    {
      SetColor(i);
      DrawLine(i,0, i,255);
    }
}


void check_me(Widget w, void *data)
{
  MyProgram *me=data;
  static int toggle=0;

  toggle ^= 1;
  SetMenuItemChecked(w, toggle);
  if (toggle)
    SetWidgetState(me->copy_menu, FALSE);
  else
    SetWidgetState(me->copy_menu, TRUE);
}


void write_ppm(char *name, char *data, int width, int height)
{
  FILE *fp;

  fp = fopen(name, "wb");
  if (fp == NULL)
    return;

  fprintf(fp, "P5\n%d %d\n255\n", width, height);
  fwrite(data, width*height, 1, fp);

  fclose(fp);
}
  

void more_stuff(Widget w, void *data)
{
  MyProgram *me=data;
  char *img;
  int width=100,height=100;
  
  printf("More stuff...\n");

  img = (char *)malloc(width*height);
  if (img == NULL)
    return;

  GetImage(img, 25, 25, width, height);

  write_ppm("foo.ppm", img, width,height);

  ScrollDrawArea(0, FontHeight(me->draw_font), 0,0, 300,300);
}


void toggle1(Widget w, void *data)
{
  MyProgram *me = (MyProgram *)data;

  me->toggle1 ^= 1;

  printf("toggle 1 changing state: %d\n", me->toggle1);
}

void toggle2(Widget w, void *data)
{
  MyProgram *me = (MyProgram *)data;
  
  me->toggle2 ^= 1;
  
  printf("toggle 2 changing state: %d\n", me->toggle2);
}


void toggle3(Widget w, void *data)
{
  MyProgram *me = (MyProgram *)data;

  me->toggle3 ^= 1;
  
  printf("toggle 3 changing state: %d\n", me->toggle3);
}


void toggle4(Widget w, void *data)
{
  MyProgram *me = (MyProgram *)data;

  me->toggle4 ^= 1;
  
  printf("toggle 4 changing state: %d\n", me->toggle4);
}


void other_toggle(Widget w, void *data)
{
  MyProgram *me = (MyProgram *)data;
  
  me->other_toggle ^= 1;
  
  printf("Other toggle changing state: %d\n", me->other_toggle);
}


void menu_item1(Widget w, void *data)
{
  printf("menu item 1 chosen\n");
}


void menu_item2(Widget w, void *data)
{
  printf("menu item 2 chosen\n");
}


void menu_item3(Widget w, void *data)
{
  printf("menu item 3 chosen\n");
}


void menu_item4(Widget w, void *data)
{
  printf("menu item 4 chosen\n");
}



/*
 * The following functions are callbacks for the drawing area widgets.
 *
 */


void redisplay(Widget w, int new_width, int new_height, void *data)
{
  MyProgram *me = data;

  width  = new_width;                 /* save new window size */
  height = new_height;
  
  do_redisplay(me, width, height);
}



void button_down(Widget w, int which_button, int x, int y, void *data)
{
  MyProgram *me=data;
  
  printf("You pressed mouse button %d at (%d,%d)\n", which_button, x,y);

  me->down = which_button;
  me->oldx = me->startx = x;
  me->oldy = me->starty = y;

  SetMouseMotionCB(w, motion);
	SetLineStyle(LineOnOffDash);
  SetDrawMode(SANE_XOR);
  SetColor(me->col1);
  SetBgColor(me->draw_widget, WHITE);
	SetLineStyle(LineSolid);
}


void button_up(Widget w, int which_button, int x, int y, void *data)
{
  MyProgram *me=data;
  
  printf("You released mouse button %d at (%d,%d)\n", which_button, x,y);

  me->down = 0;
  SetDrawMode(GXcopy);   /* sets things back to normal */
  SetMouseMotionCB(w, NULL);
}




/*
 * The following function is called when keypresses happen in the drawing
 * area widget.
 *
 *  It is useful to know that the string returned to your program is
 * not necessarily a single ASCII character. You will get the usual
 * ASCII characters, including control characters (such as ^C or ^H).
 * But, the workstation's function keys will also be returned in a
 * string such as "F11" or "F23".  You will also get other longer
 * strings such as "Control_L", "Alt_R", or "Shift_L".  It is
 * important to understand that even if you just press the shift key to
 * get a capital letter, you will first receive the string "Shift_L"
 * or "Shift_R", then you will receive a capital letter (say, "H").
 * You should probably ignore the "Shift_L" or "Shift_R" messages (but
 * who knows, you may find some use for them).
 *
 * The argument, up_or_down, tells you whether the given key was pressed
 * or released.  If the key was pressed down, up_or_down has a zero (0),
 * if the key was released, up_or_down contains a 1.
 *
 *
 * NOTE WELL:
 *   The string that is returned to you can _NOT_ (I'll repeat that,
 *   can _NOT_) be modified by your program.  Got it?  Do _NOT_ modify
 *   the string.  If you want to munge with it, make a copy using
 *   strdup() or strcpy() into your own buffer space.
 */

void keypress(Widget w, char *input, int up_or_down, void *data)
{
  char *str;

  if (input == NULL)
    return;

  if (up_or_down == 0)
    str = "Down";
  else
    str = "up";
  
  if (isprint(*input))
    printf("Key: <<%s>> %s\n", input, str);
  else
    printf("Key: (decimal value %d (0x%x) %s\n", *input, *input, str);
    
}




/* Called when a DrawingArea gets mouse motion events.  The arguments
 * X and Y are the current position of the mouse in the window.
 *
 * NOTE: This routine should be _fast_ because *every* single time
 *       the mouse moves in the drawing area, this function is called. 
 *       So even if you are just moving the mouse across the drawing area,
 *       this routine is called. As it stands, it doesn't do anything.
 */
void motion(Widget w, int x, int y, void *data)
{
  MyProgram *me=data;
  int owidth, oheight;
  
  owidth  = me->oldx - me->startx;
  oheight = me->oldy - me->starty;
  
  if (me->down == 1)                  /* draw box outlines */
   {
     DrawBox(me->startx, me->starty, owidth, oheight);
     DrawBox(me->startx, me->starty, (x - me->startx), (y - me->starty));
   }
  else if (me->down == 2)             /* draw some circles */
   {
     DrawArc(me->startx, me->starty, owidth, oheight, 0, 360);
     DrawArc(me->startx, me->starty, (x-me->startx),(y-me->starty), 360,360);
   }
  else if (me->down == 3)             /* draw some filled boxes */
   {
     DrawFilledBox(me->startx, me->starty, owidth, oheight);
     DrawFilledBox(me->startx, me->starty, (x - me->startx), (y - me->starty));
   }

  me->oldx = x;
  me->oldy = y;
}


/* Called the pointer enters a Drawing Area. The arguments X and Y
 * are the coordinates of the entry point.
 */
void enter(Widget w, int x, int y, void *data)
{
  /* printf("Entered drawing area at (%d, %d)\n", x, y); */
}

/* Called the pointer leaves a Drawing Area. The arguments X and Y
 * are the coordinates of the exit point.
 */
void leave(Widget w, int x, int y, void *data)
{
  /* printf("Exited  drawing area at (%d, %d)\n", x, y);  */
}