File: exgui.c

package info (click to toggle)
allegro4 2%3A4.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 17,052 kB
  • ctags: 12,972
  • sloc: ansic: 109,525; asm: 16,672; cpp: 3,221; sh: 1,761; makefile: 556; pascal: 105; perl: 73
file content (144 lines) | stat: -rw-r--r-- 4,275 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
/*
 *    Example program for the Allegro library, by Shawn Hargreaves.
 *
 *    This program demonstrates how to use the GUI routines.
 */


#include <stdio.h>

#include "allegro.h"
#include "example.h"



/* we need to load example.dat to access the big font */
DATAFILE *datafile;


/* for the d_edit_proc() object */
char the_string[32] = "Change Me!";


/* since we change the font, we need to store a copy of the original one */
FONT *original_font;



/* A custom dialog procedure for the 'change font' button. This uses a
 * simple form of inheritance: it calls d_button_proc() to do most of
 * the work, so it behaves exactly like any other button, but when the
 * button is clicked and d_button_proc() returns D_CLOSE, it intercepts
 * the message and changes the font instead.
 */
int change_font_proc(int msg, DIALOG *d, int c)
{
   int ret;

   /* call the parent object */
   ret = d_button_proc(msg, d, c);

   /* trap the close return value and change the font */
   if (ret == D_CLOSE) {
      if (font == original_font)
	 font = datafile[BIG_FONT].dat;
      else
	 font = original_font;

      return D_REDRAW; 
   }

   /* otherwise just return */
   return ret;
}



/* callback function to specify the contents of the listbox */
char *listbox_getter(int index, int *list_size)
{
   static char *strings[] =
   {
      "Zero",  "One",   "Two",   "Three", "Four",  "Five", 
      "Six",   "Seven", "Eight", "Nine",  "Ten"
   };

   if (index < 0) {
      *list_size = 11;
      return NULL;
   }
   else
      return strings[index]; 
}



DIALOG the_dialog[] =
{
   /* (dialog proc)     (x)   (y)   (w)   (h)   (fg)  (bg)  (key) (flags)  (d1)                    (d2)  (dp)              (dp2) (dp3) */
   { d_clear_proc,      0,    0,    0,    0,    255,  0,    0,    0,       0,                      0,    NULL,             NULL, NULL  },
   { d_edit_proc,       80,   32,   512,  48,   255,  0,    0,    0,       sizeof(the_string)-1,   0,    the_string,       NULL, NULL  },
   { d_button_proc,     80,   132,  161,  49,   255,  0,    't',  0,       0,                      0,    "&Toggle Me",     NULL, NULL  },
   { d_list_proc,       360,  100,  207,  207,  255,  0,    0,    0,       0,                      0,    listbox_getter,   NULL, NULL  },
   { change_font_proc,  80,   232,  161,  49,   255,  0,    'f',  D_EXIT,  0,                      0,    "Change &Font",   NULL, NULL  },
   { d_button_proc,     80,   400,  161,  49,   255,  0,    0,    D_EXIT,  0,                      0,    "OK",             NULL, NULL  },
   { d_button_proc,     360,  400,  161,  49,   255,  0,    0,    D_EXIT,  0,                      0,    "Cancel",         NULL, NULL  },
   { NULL,              0,    0,    0,    0,    0,    0,    0,    0,       0,                      0,    NULL,             NULL, NULL  }
};



/* index of the listbox object in the dialog array */
#define LISTBOX_OBJECT     3



int main(int argc, char *argv[])
{
   char buf1[256], buf2[80], buf3[80];
   int ret;

   /* initialise everything */
   allegro_init();
   install_keyboard(); 
   install_mouse();
   install_timer();
   if (set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
      return 1;
   }
   set_palette(desktop_palette);

   /* We set up colors to match screen color depth (in case it changed) */
   for (ret = 0; the_dialog[ret].proc; ret++) {
      the_dialog[ret].fg = makecol(0, 0, 0);
      the_dialog[ret].bg = makecol(255, 255, 255);
   }

   /* load the datafile */
   replace_filename(buf1, argv[0], "example.dat", sizeof(buf1));
   datafile = load_datafile(buf1);
   if (!datafile) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Error loading %s!\n", buf1);
      return 1;
   }

   /* store a copy of the default font */
   original_font = font;

   /* do the dialog */
   ret = do_dialog(the_dialog, -1);

   /* and report the results */
   sprintf(buf1, "do_dialog() returned %d", ret);
   sprintf(buf2, "string is '%s'", the_string);
   sprintf(buf3, "listbox selection is %d", the_dialog[LISTBOX_OBJECT].d1);
   alert(buf1, buf2, buf3, "OK", NULL, 0, 0);

   unload_datafile(datafile);
   return 0;
}

END_OF_MAIN();