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
| --***************************************************************
-- Nom du programme : Calculatrice
-- Type : Interface
-- Sujet : Calculatrice construite à partir de GTKADA
--
-- Auteur : Pascal RICHARD
-- Version : 5.0
-- Création : 08/03/2007
-- Dernière modif. : 15/05/2007
--
-- Outils : AIDE - GPS - MSys
-- Remarques : Environnement Windows XP SP2
--****************************************************************
with Glib; use Glib;
with Gtk.Main; use Gtk.Main;
with Gtk.Window; use Gtk.Window;
with Gtk.Frame; use Gtk.Frame;
with Gtk.GEntry; use Gtk.GEntry;
with Gtk.Enums; use Gtk.Enums;
with Gtk.Button; use Gtk.Button;
with Gtk.Check_Button; use Gtk.Check_Button;
with Gtk.Table; use Gtk.Table;
with Gtk.Handlers; use Gtk.Handlers;
with gtk.widget; use gtk.widget;
with gtk.editable; use gtk.editable;
with p_calculs; use p_calculs;
with ada.strings.unbounded; use ada.strings.unbounded;
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
with p_fonctions; use p_fonctions;
with p_pile; use p_pile;
with p_operations; use p_operations;
procedure cessai2 is
Table : gtk_table;
Fenetre : gtk_window;
Ecran, Affich_result : Gtk_GEntry;
Frame_affich, Frame_ope, Frame_nomb : gtk_frame;
btn_plus, btn_moins, btn_mul, btn_div : Gtk_Button;
btn_clear, btn_exe : Gtk_Button;
bouton0, bouton1, bouton2 : gtk_button;
pos : Gint := -1;
a : string := "";
U_result : unbounded_string;
--bouton: array(0 .. 1) of Gtk_Button;
G : constant array(0 .. 2) of Guint := (17, 14, 17);
D : constant array(0 .. 2) of Guint := (20, 17, 20);
H : constant array(0 .. 2) of Guint := (17, 14, 14);
B : constant array(0 .. 2) of Guint := (19, 16, 16);
------------------------------------------
-- package aff_chiffres (spécification) --
------------------------------------------
package aff_chiffres is
procedure affich0 (emet : access gtk_widget_record'class);
procedure affich1 (emet : access gtk_widget_record'class);
procedure affich2 (emet : access gtk_widget_record'class);
end aff_chiffres;
----------------------------------
-- package aff_chiffres (corps) --
----------------------------------
package body aff_chiffres is
procedure affich0 (emet : access gtk_widget_record'class) is
begin
insert_text(Ecran, "0", pos); -- fonction issue de Gtk.Editable
end affich0;
procedure affich1 (emet : access gtk_widget_record'class) is
begin
insert_text(Ecran, "1", pos);
end affich1;
procedure affich2 (emet : access gtk_widget_record'class) is
begin
insert_text(Ecran, "2", pos);
end affich2;
end aff_chiffres;
----------------------------------------
-- package aff_outils (spécification) --
----------------------------------------
package aff_outils is
-- Actions --
procedure affich_plus (emet : access gtk_widget_record'class);
procedure affich_moins (emet : access gtk_widget_record'class);
procedure affich_mul (emet : access gtk_widget_record'class);
procedure affich_div (emet : access gtk_widget_record'class);
procedure effacer (emet : access gtk_widget_record'class);
procedure aff_resultat (emet : access gtk_widget_record'class);
-- procedure spliter_gch (UStr : unbounded_string);
-- procedure spliter_drt (UStr : unbounded_string);
-- procedure spliter_sig (UStr : unbounded_string);
function spliter_gch (UStr : unbounded_string) return integer;
function spliter_drt (UStr : unbounded_string) return integer;
function spliter_sig (UStr : unbounded_string) return character;
function operation return integer;
function recup_resultat return unbounded_string;
function compte_elem return integer;
function recup_string return string;
end aff_outils;
--------------------------------
-- package aff_outils (corps) --
--------------------------------
package body aff_outils is
-- Actions --
procedure affich_plus (emet : access gtk_widget_record'class) is
begin
insert_text(Ecran, "+", pos);
end affich_plus;
procedure affich_moins (emet : access gtk_widget_record'class) is
begin
insert_text(Ecran, "-", pos);
end affich_moins;
procedure affich_mul (emet : access gtk_widget_record'class) is
begin
insert_text(Ecran, "*", pos);
end affich_mul;
procedure affich_div (emet : access gtk_widget_record'class) is
begin
insert_text(Ecran, "/", pos);
end affich_div;
procedure effacer (emet : access gtk_widget_record'class) is
begin
delete_text(Ecran, 0, pos);
end effacer;
-- procedure spliter_gch (UStr : unbounded_string) is
-- int_gch : integer;
-- begin
-- int_gch := integer'value(chaine_gauche(UStr));
-- empiler(int_gch);
-- end spliter_gch;
--
-- procedure spliter_drt (UStr : unbounded_string) is
-- int_drt : integer;
-- begin
-- int_drt := integer'value(chaine_droite(UStr));
-- empiler(int_drt);
-- end spliter_drt;
--
-- procedure spliter_sig (UStr : unbounded_string) is
-- char_sig : character;
-- begin
-- char_sig := ada.strings.unbounded.element(Ustr, cpt_elem_gch(UStr)+1);
-- empiler_sig(char_sig);
-- end spliter_sig;
function spliter_gch (UStr : unbounded_string) return integer is
int_gch : integer;
begin
int_gch := integer'value(chaine_gauche(UStr));
return int_gch;
end spliter_gch;
function spliter_drt (UStr : unbounded_string) return integer is
int_drt : integer;
begin
int_drt := integer'value(chaine_droite(UStr));
return int_drt;
end spliter_drt;
function spliter_sig (UStr : unbounded_string) return character is
char_sig : character;
begin
char_sig := ada.strings.unbounded.element(Ustr, cpt_elem_gch(UStr)+1);
return char_sig;
end spliter_sig;
function operation return integer is
Uchaine : unbounded_string := ada.strings.unbounded.to_unbounded_string(get_chars(Ecran, 0, pos));
gauche, droite, resultat : integer;
sig : character;
begin
gauche := spliter_gch(Uchaine);
droite := spliter_drt(Uchaine);
sig := spliter_sig(Uchaine);
if sig = '+' then
resultat := gauche + droite;
elsif sig = '-' then
resultat := gauche - droite;
elsif sig = '*' then
resultat := gauche * droite;
else
resultat := 0;
end if;
-- -- return resultat;
-- case sig is
-- when '+' => resultat := gauche + droite;
-- when '-' => resultat := gauche - droite;
-- when '/' => resultat := gauche / droite;
-- when '*' => resultat := gauche * droite;
-- when others => null;
-- end case;
return resultat;
end operation;
function recup_resultat return unbounded_string is
int_ustr : unbounded_string;
resultat : integer;
begin
resultat := operation;
int_ustr := ada.strings.unbounded.to_unbounded_string(integer'image(resultat));
return int_ustr;
end recup_resultat;
function compte_elem return integer is
long : integer := explode(recup_resultat);
begin
return long;
end compte_elem;
function recup_string return string is
chaine : string (1 .. compte_elem);
begin
chaine := ada.strings.unbounded.to_string(recup_resultat);
return chaine;
end recup_string;
procedure aff_resultat (emet : access gtk_widget_record'class) is
chaine : string (1 .. compte_elem);
begin
chaine := recup_string;
insert_text(affich_result, chaine, pos);
end aff_resultat;
end aff_outils;
package traitement is new callback (gtk_widget_record);
package traitement_tools is new callback (gtk_widget_record);
-- type String_Ptr is access all String;
--
-- procedure Affich (Emet : access Gtk_Widget_Record'Class;
-- Data : String_Ptr) is
-- begin
-- Insert_Text(Ecran, Data.all , Pos);
-- end affich;
--
-- package Traitement is new Gtk.Handlers.User_Callback
-- (Widget_Type => Gtk_Widget_Record,
-- User_Type => String_Ptr);
use aff_outils;
begin
init;
-- Création de la fenetre
gtk_new(fenetre, window_toplevel);
set_size_request(fenetre, 400, 300);
set_position(fenetre, win_pos_center);
gtk_new(Table, 21, 25, true);
add(fenetre, Table);
-- Création d'un Frame "Affichage", suivi
-- de la Création de l'écran d'affichage
gtk_new(Frame_affich, "Affichage");
attach_defaults(Table, Frame_affich, 1, 24, 1, 6);
gtk_new(Ecran);
attach_defaults(Table, Ecran, 2, 9, 2, 3);
set_has_frame (Ecran, false);
set_editable (Ecran, False);
-- gtk_new(Ecran2);
-- attach_defaults(Table, Ecran2, 9, 12, 2, 3);
-- set_has_frame (Ecran2, false);
-- set_editable (Ecran2, False);
--
-- gtk_new(Ecran3);
-- attach_defaults(Table, Ecran3, 12, 19, 2, 3);
-- set_has_frame (Ecran3, false);
-- set_editable (Ecran3, False);
gtk_new(Affich_result);
attach_defaults(Table, Affich_result, 2, 19, 4, 5);
set_editable (Affich_result, False);
gtk_new(btn_exe, "EXE");
attach_defaults(Table, btn_exe, 20, 23, 2, 5);
traitement_tools.connect (btn_exe, "clicked", traitement_tools.to_marshaller(aff_outils.aff_resultat'access));
-- Création des boutons du frame "Opérations" - opérations usuelles
-- suivi de la création des boutons relatifs à ce frame
gtk_new(Frame_ope, "Operations");
attach_defaults(Table, frame_ope, 1, 12, 13, 20);
gtk_new(btn_plus, "+");
attach_defaults(Table, btn_plus, 3, 5, 15, 17);
traitement_tools.connect (btn_plus, "clicked", traitement_tools.to_marshaller(aff_outils.affich_plus'access));
gtk_new(btn_moins, "-");
attach_defaults(Table, btn_moins, 5, 7, 15, 17);
traitement_tools.connect (btn_moins, "clicked", traitement_tools.to_marshaller(aff_outils.affich_moins'access));
gtk_new(btn_mul, "*");
attach_defaults(Table, btn_mul, 7, 9, 15, 17);
traitement_tools.connect (btn_mul, "clicked", traitement_tools.to_marshaller(aff_outils.affich_mul'access));
gtk_new(btn_div, "/");
attach_defaults(Table, btn_div, 9, 11, 15, 17);
traitement_tools.connect (btn_div, "clicked", traitement_tools.to_marshaller(aff_outils.affich_div'access));
-- Création du Frame "Nombres" suivi de la
-- création des boutons "virgule" et "puissance de 10", suivi de
-- la création d'une boucle pour générer les x boutons chiffres
gtk_new(Frame_nomb, "Chiffres");
attach_defaults(Table, Frame_nomb, 13, 24, 7, 20);
gtk_new(btn_clear, "C");
attach_defaults(Table, btn_clear, 20, 23, 17, 19);
traitement_tools.connect (btn_clear, "clicked", traitement_tools.to_marshaller(aff_outils.effacer'access));
gtk_new(bouton0, "0");
attach_defaults(Table, bouton0, g(0), d(0), h(0), b(0));
traitement.connect (bouton0, "clicked", traitement.to_marshaller(aff_chiffres.affich0'access));
gtk_new(bouton1, "1");
attach_defaults(Table, bouton1, g(1), d(1), h(1), b(1));
traitement.connect (bouton1, "clicked", traitement.to_marshaller(aff_chiffres.affich1'access));
gtk_new(bouton2, "2");
attach_defaults(Table, bouton2, g(2), d(2), h(2), b(2));
traitement.connect (bouton2, "clicked", traitement.to_marshaller(aff_chiffres.affich2'access));
-- for J in Bouton'Range loop
-- Gtk_New(Bouton(J), Integer'Image(J));
-- Traitement.Connect(Bouton(J),
-- "clicked",
-- Traitement.To_Marshaller(Affich'Access),
-- new String'(get_label(bouton(J)))); --Integer'Image(J)));
-- end loop;
--
--
-- for J in bouton'range loop
-- attach_defaults(Table, bouton(J), G(J), D(J), H(J), B(J));
-- end loop;
show_all(fenetre);
main;
end cessai2; |
Partager