Re: [AD] excustom

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On 2005-08-23, Peter Wang <tjaden@xxxxxxxxxx> wrote:
> 
> Please commit the whitespace changes separately though.

I've saved you the trouble of making a new patch; attached.

Peter
Index: excustom.c
===================================================================
RCS file: /cvsroot/alleg/allegro/examples/excustom.c,v
retrieving revision 1.13
diff -u -b -r1.13 excustom.c
--- excustom.c	13 Feb 2005 21:01:29 -0000	1.13
+++ excustom.c	23 Aug 2005 07:22:30 -0000
@@ -2,15 +2,18 @@
  *    Example program for the Allegro library, by Shawn Hargreaves.
  *
  *    A follow up of the exgui.c example showing how to customise the
- *    default Allegro framework. In this case a dialog procedure
- *    animates a graphical clock without disrupting other GUI
- *    dialogs.  A more simple option shows how to dynamically change
- *    the font used by all GUI elements.
+ *    default Allegro framework, for example, how to use a custom
+ *    font. It also shows a dialog procedure which animates a
+ *    graphical clock without disrupting other GUI dialogs. This is
+ *    especially tricky with Allegro's menus, since the active dialog
+ *    gets shut down as long as the menu is handled. The only way
+ *    around it, as is demonstrated here, is to forcefully update the
+ *    dialog yourself.
  */
 
 
 #include <time.h>
-
+#include <stdio.h>
 #include <allegro.h>
 #include "example.h"
 
@@ -140,19 +143,8 @@
 	     (the_time.tm_min != t->tm_min) ||
 	     (the_time.tm_hour != t->tm_hour)) {
 	    the_time = *t;
-
-	    /* Redraw ourselves if the time has changed. Note that the dialog
-	     * manager automatically turns off the mouse pointer whenever a
-	     * MSG_DRAW message is sent to an individual object or an entire
-	     * dialog, so we don't have to do it explicitly. Also note the use
-	     * of the object_message function rather than a simple recursive
-	     * call to clock_proc(). This vectors the call through the function
-	     * pointer in the dialog object, which allows other object
-	     * procedures to hook it, for example a different type of clock
-	     * could process the draw messages itself but pass idle messages
-	     * on to this procedure.
-	     */
-	    object_message(d, MSG_DRAW, 0);
+            /* forcefully redraw, in case the menu is active */
+            clock_proc(MSG_DRAW, d, 0);
 	 }
 	 break;
 
@@ -183,15 +175,35 @@
 
 
 
+MENU sub_menu[] =
+{
+   { "Test &1",   NULL,  NULL,      0,  NULL  },
+   { "Test &2",   NULL,  NULL,      0,  NULL  },
+   { NULL,        NULL,  NULL,      0,  NULL  }
+};
+
+
+
+MENU the_menu[] =
+{
+   { "Test &1",   NULL, sub_menu,  0,  NULL  },
+   { "Test &2",   NULL, sub_menu,  0,  NULL  },
+   { NULL,        NULL, NULL,      0,  NULL  }
+};
+
+
+
 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,       12,   82,   256,  48,   255,  0,    0,    0,       LEN, 0,    the_string,    NULL, NULL  },
-   { d_check_proc,      12,   12,   161,  49,   255,  0,    't',  0,       0,   0,    "&Toggle Me",  NULL, NULL  },
-   { clock_proc,        242,  12,   64,   64,   255,  0,    0,    0,       0,   0,    NULL,          NULL, NULL  },
-   { change_font_proc,  12,   142,  141,  49,   255,  0,    'f',  D_EXIT,  0,   0, "Change &Font",   NULL, NULL  },
-   { d_button_proc,     162,  142,  141,  49,   255,  0,    0,    D_EXIT,  0,   0,    "Exit",        NULL, NULL  },
+   { d_menu_proc,       10,   10,   300,  50,   255,  0,    0,    0,       0,   0,    the_menu,      NULL, NULL  },
+   { d_edit_proc,       10,   70,   300,  50,   255,  0,    0,    0,       LEN, 0,    the_string,    NULL, NULL  },
+   { d_check_proc,      10,   140,  300,  50,   255,  0,    't',  0,       0,   0,    "&Toggle Me",  NULL, NULL  },
+   { clock_proc,        430,  10,   200,  200,  255,  0,    0,    0,       0,   0,    NULL,          NULL, NULL  },
+   { change_font_proc,  10,   420,  300,  50,   255,  0,    'f',  D_EXIT,  0,   0, "Change &Font",   NULL, NULL  },
+   { d_button_proc,     330,  420,  300,  50,   255,  0,    0,    D_EXIT,  0,   0,    "Exit",        NULL, NULL  },
+   { d_yield_proc,      0,    0,    0,    0,    0,    0,    0,    0,       0,   0,    NULL,          NULL, NULL  },
    { NULL,              0,    0,    0,    0,    0,    0,    0,    0,       0,   0,    NULL,          NULL, NULL  }
 };
 
@@ -201,6 +213,7 @@
 {
    int item;
    char buf[256];
+   DIALOG_PLAYER *player;
 
    if (allegro_init() != 0)
       return 1;
@@ -208,8 +221,8 @@
    install_mouse();
    install_timer();
 
-   if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0) {
-      if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
+   if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) {
+      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;
@@ -236,7 +249,19 @@
    /* store a copy of the default font */
    original_font = font;
 
-   do_dialog(the_dialog, -1);
+   font = datafile[BIG_FONT].dat;
+
+   show_mouse(screen);
+   player = init_dialog(the_dialog, -1);
+   while (update_dialog(player)) {
+      DIALOG *d;
+      for (d = the_dialog; d->proc; d++) {
+         /* These two can safely be called even while the menu is active. */
+         if (d->proc == d_yield_proc || d->proc == clock_proc)
+            object_message(d, MSG_IDLE, 0);
+      }
+   }
+   shutdown_dialog(player);
    
    unload_datafile (datafile);
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/