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
|
/* This file contains callback routines that implement the logic of your
* program. The function init_display() in main.c built the display
* and setup the connection between these functions and the user interface
* elements.
*
* 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
* dbg@sgi.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include "libsx.h"
#include "main.h"
#include "time.h"
#include "callbacks.h"
/* -- CALLBACK ROUTINES --
*
* These functions are called when various things happen in your windows.
* They should handle what happened and give feedback to the user. Most
* likely they will set options in your program (which is what the
* MyProgram data structure is for), and cause different things to happen,
* like loading and saving files, etc.
*/
void quit(Widget w, void *data)
{
/* Do any cleanup that is necessary for your program here */
exit(0);
}
void load(Widget w, void *data)
{
char *string;
string = GetString("\nEnter a filename to load\n", "untitled");
if (string)
printf("You entered the name: %s\n", string);
else
printf("You clicked the cancel button.\n");
}
void click_me(Widget w, void *data)
{
MyProgram *me = (MyProgram *)data;
int ans;
ans = GetYesNo("\nAre you a weenie?\n\n");
if (ans)
{
me->var1 = TRUE;
printf("The user is a weenie.\n");
}
else
{
me->var2 = FALSE;
printf("You are not a weenie.\n");
}
}
/*
* The following is the redisplay code for the drawing area widget.
*
* Each time it needs to be redisplayed (either because it the window
* was resized or because it was obscured), this function gets called.
*/
void redisplay(Widget w, int width, int height, void *data)
{
ClearDrawArea(); /* start with a clean slate */
draw_stuff(width, height);
}
void draw_stuff(int width, int height)
{
draw_lines(width, height);
draw_random_lines();
draw_points();
draw_text(width, height);
}
void draw_lines(int width, int height)
{
int i;
SetColor(BLACK); /* draw some pretty patterns */
for(i=0; i < width; i+=5)
DrawLine(0,i, i,height);
SetColor(GREEN);
for(i=0; i < width; i+=5)
DrawLine(width,i, width-i,height);
}
void draw_random_lines(void)
{
int i;
XPoint xpts[50];
srand(time(NULL) | 0x01); /* seed the random number generator */
for(i=0; i < 50; i++)
{
xpts[i].x = (rand() % 75) + 75; /* pick random vertices */
xpts[i].y = (rand() % 75) + 75;
}
SetColor(RED);
DrawPolyline(xpts, 50); /* now draw all 50 of them in red */
}
void draw_points(void)
{
int i, x,y;
srand(time(NULL) | 0x01); /* seed the random number generator */
SetColor(BLUE);
for(i=0; i < 100; i++)
{
x = (rand() % 75) + 150; /* pick a random point */
y = (rand() % 75) + 75;
DrawPixel(x, y); /* now draw it on the screen */
}
}
void draw_text(int width, int height)
{
SetColor(BLACK);
DrawText("My Cool Program", (width/2)-50, height/2);
}
|