GRASS Programmer's Manual  6.4.2(2012)
display/window.c
Go to the documentation of this file.
00001 /*
00002  * D_new_window(name, t, b, l, r)
00003  *   creates a new window with given coordinates
00004  *   if "name" is an empty string, the routine returns a unique
00005  *   string in "name"
00006  *
00007  * D_reset_screen_window(t, b, l, r)
00008  *   resets the edges of the current window
00009  *
00010  * D_set_cur_wind(name)
00011  *   saves "name" in cur_w field in "no-name" pad
00012  *   outlines previous current window in GRAY
00013  *   outlines "name" in DEFAULT_FG_COLOR
00014  *
00015  * D_get_cur_wind(name)
00016  *   gets the current name stored in cur_w field in "no_name" pad
00017  *
00018  * D_show_window(color)
00019  *   outlines current window in color (from ../colors.h)
00020  *
00021  * D_get_screen_window(t, b, l, r)
00022  *   returns current window's coordinates 
00023  *
00024  * D_check_map_window(wind)
00025  *   if map window (m_win) already assigned
00026  *       map window is read into the struct "wind"
00027  *   else
00028  *       struct "wind" is written to map window (m_win)
00029  *
00030  * D_remove_window()
00031  *   remove any trace of window
00032  *
00033  * D_erase_window()
00034  *   Erases the window on scree.  Does not affect window contents list.
00035  */
00036 
00037 #include <string.h>
00038 #include <grass/colors.h>
00039 #include <grass/gis.h>
00040 #include <grass/display.h>
00041 #include <grass/raster.h>
00042 
00043 
00060 int D_new_window(char *name, int t, int b, int l, int r)
00061 {
00062     int stat;
00063     char buff[256];
00064 
00065     /* If no name was sent, get a unique name for the window */
00066     if (!*name)
00067         R_pad_invent(name);
00068 
00069     /* Create the work pad */
00070     if ((stat = R_pad_create(name))) {
00071         R_pad_perror(name, stat);
00072         return (-1);
00073     }
00074 
00075     /* Select work pad for use */
00076     if ((stat = R_pad_select(name)))
00077         goto pad_error;
00078 
00079     /* Timestamp current pad */
00080     D_timestamp();
00081 
00082     sprintf(buff, "%d %d %d %d", t, b, l, r);
00083     if ((stat = R_pad_set_item("d_win", buff)))
00084         goto pad_error;
00085 
00086     /* Display outline of new window */
00087     D_show_window(GRAY);
00088 
00089     R_set_window(t, b, l, r);
00090 
00091     return (0);
00092 
00093   pad_error:
00094     R_pad_delete();
00095     sprintf(buff, "window <%s>, item <%s>", name, "d_win");
00096     R_pad_perror(buff, stat);
00097 
00098     return (-1);
00099 }
00100 
00101 
00118 int D_new_window_percent(char *name, float b, float t, float l, float r)
00119 {
00120     int scr_t = R_screen_top();
00121     int scr_b = R_screen_bot();
00122     int scr_l = R_screen_left();
00123     int scr_r = R_screen_rite();
00124 
00125     int win_t = 0.5 + scr_t + (scr_b - scr_t) * (100. - t) / 100.0;
00126     int win_b = 0.5 + scr_t + (scr_b - scr_t) * (100. - b) / 100.0;
00127     int win_l = 0.5 + scr_l + (scr_r - scr_l) * l / 100.0;
00128     int win_r = 0.5 + scr_l + (scr_r - scr_l) * r / 100.0;
00129 
00130     if (win_t < scr_t)
00131         win_t = scr_t;
00132     if (win_b > scr_b)
00133         win_b = scr_b;
00134     if (win_l < scr_l)
00135         win_l = scr_l;
00136     if (win_r > scr_r)
00137         win_r = scr_r;
00138 
00139     return D_new_window(name, win_t, win_b, win_l, win_r);
00140 }
00141 
00142 
00154 int D_set_cur_wind(const char *name)
00155 {
00156     char pad_cur[64];
00157     int stat;
00158     int not_same_window;
00159     int t, b, l, r;
00160 
00161     /* Abort if window name is null */
00162     if (!strlen(name))
00163         return (-1);
00164 
00165     /* Abort if window name is not available */
00166     if ((stat = R_pad_select(name)))
00167         return (stat);
00168 
00169     /* Get name of current window pad */
00170     D_get_cur_wind(pad_cur);
00171 
00172     /* Establish whether it is the same as the currently selected pad */
00173     if (strlen(pad_cur)) {
00174         not_same_window = strcmp(name, pad_cur);
00175         if (not_same_window) {
00176             R_pad_select(pad_cur);
00177             D_show_window(GRAY);
00178         }
00179     }
00180     else {
00181         not_same_window = 1;
00182     }
00183 
00184     if (not_same_window) {
00185         /* Delete the current window name in no-name pad */
00186         R_pad_select("");
00187         if ((stat = R_pad_delete_item("cur_w")))
00188             return (stat);
00189 
00190         /* Update the current window name in no-name pad */
00191         if ((stat = R_pad_set_item("cur_w", name)))
00192             return (stat);
00193 
00194         /* Select new window pad */
00195         if ((stat = R_pad_select(name)))
00196             return (stat);
00197 
00198         /* Outline new window in highlight color */
00199         D_show_window(D_translate_color(DEFAULT_FG_COLOR));
00200 
00201         /* Tell driver of current window */
00202         D_get_screen_window(&t, &b, &l, &r);
00203         R_set_window(t, b, l, r);
00204     }
00205     else {
00206         /* Select new window pad */
00207         if ((stat = R_pad_select(name)))
00208             return (stat);
00209     }
00210 
00211     return (0);
00212 }
00213 
00214 
00224 int D_get_cur_wind(char *name)
00225 {
00226     int count;
00227     int stat;
00228     char **list;
00229 
00230     if ((stat = R_pad_select("")))
00231         return (stat);
00232 
00233     if ((stat = R_pad_get_item("cur_w", &list, &count))) {
00234         strcpy(name, "");
00235         return (stat);
00236     }
00237 
00238     strcpy(name, list[0]);
00239     R_pad_freelist(list, count);
00240     R_pad_select(name);
00241     return (0);
00242 }
00243 
00244 
00258 int D_show_window(int color)
00259 {
00260     int t, b, l, r;
00261     int stat;
00262 
00263     if ((stat = D_get_screen_window(&t, &b, &l, &r)))
00264         return (stat);
00265 
00266     R_set_window(t - 1, b + 1, l - 1, r + 1);
00267 
00268     R_standard_color(color);
00269     R_move_abs(l - 1, b);
00270     R_cont_abs(l - 1, t - 1);
00271     R_cont_abs(r, t - 1);
00272     R_cont_abs(r, b);
00273     R_cont_abs(l - 1, b);
00274     R_flush();
00275 
00276     R_set_window(t, b, l, r);
00277 
00278     return (0);
00279 }
00280 
00281 
00295 int D_get_screen_window(int *t, int *b, int *l, int *r)
00296 {
00297     int stat;
00298     int count;
00299     char **list;
00300 
00301     if ((stat = R_pad_get_item("d_win", &list, &count)))
00302         return (stat);
00303 
00304     sscanf(list[0], "%d %d %d %d", t, b, l, r);
00305 
00306     R_pad_freelist(list, count);
00307 
00308     return (0);
00309 }
00310 
00311 
00326 int D_check_map_window(struct Cell_head *wind)
00327 {
00328     char buff[256];
00329     char ebuf[64], nbuf[64], sbuf[64], wbuf[64];
00330     int num;
00331     int count;
00332     char **list;
00333     char *err;
00334 
00335     if (0 != R_pad_get_item("m_win", &list, &count)) {
00336         G_format_easting(wind->east, ebuf, wind->proj);
00337         G_format_easting(wind->west, wbuf, wind->proj);
00338         G_format_northing(wind->north, nbuf, wind->proj);
00339         G_format_northing(wind->south, sbuf, wind->proj);
00340         sprintf(buff, "%d %d %s %s %s %s %d %d",
00341                 wind->proj, wind->zone,
00342                 ebuf, wbuf, nbuf, sbuf, wind->rows, wind->cols);
00343         if (R_pad_set_item("m_win", buff))
00344             return (-1);
00345         return (0);
00346     }
00347     else {
00348         num = sscanf(list[0], "%d %d %s %s %s %s %d %d",
00349                      &wind->proj, &wind->zone,
00350                      ebuf, wbuf, nbuf, sbuf, &wind->rows, &wind->cols);
00351 
00352         R_pad_freelist(list, count);
00353 
00354         if (num != 8)
00355             return -2;
00356 
00357         if (!G_scan_easting(ebuf, &wind->east, wind->proj))
00358             return -2;
00359         if (!G_scan_easting(wbuf, &wind->west, wind->proj))
00360             return -2;
00361         if (!G_scan_northing(nbuf, &wind->north, wind->proj))
00362             return -2;
00363         if (!G_scan_northing(sbuf, &wind->south, wind->proj))
00364             return -2;
00365 
00366         if ((err = G_adjust_Cell_head(wind, 1, 1)))
00367             return -2;
00368 
00369         return 0;
00370     }
00371 }
00372 
00373 
00387 int D_reset_screen_window(int t, int b, int l, int r)
00388 {
00389     int stat;
00390     char buff[256];
00391 
00392     D_show_window(D_translate_color(DEFAULT_BG_COLOR));
00393 
00394     sprintf(buff, "%d %d %d %d", t, b, l, r);
00395     R_pad_delete_item("d_win");
00396     if ((stat = R_pad_set_item("d_win", buff)))
00397         return (stat);
00398 
00399     D_show_window(D_translate_color(DEFAULT_FG_COLOR));
00400 
00401     return (0);
00402 }
00403 
00404 
00416 int D_timestamp(void)
00417 {
00418     char buff[128];
00419     int stat;
00420     int count;
00421     char **list;
00422     char cur_pad[64];
00423     int cur_time;
00424 
00425     R_pad_current(cur_pad);
00426 
00427     R_pad_select("");
00428     if ((stat = R_pad_get_item("time", &list, &count))) {
00429         R_pad_set_item("time", "1");
00430         R_pad_select(cur_pad);
00431         R_pad_set_item("time", "1");
00432         return (1);
00433     }
00434 
00435     sscanf(list[0], "%d", &cur_time);
00436     sprintf(buff, "%d", cur_time + 1);
00437     R_pad_set_item("time", buff);
00438 
00439     R_pad_freelist(list, count);
00440 
00441     R_pad_select(cur_pad);
00442 
00443     R_pad_delete_item("time");
00444     return (R_pad_set_item("time", buff));
00445 }
00446 
00447 
00457 void D_remove_window(void)
00458 {
00459     R_pad_delete();
00460     R_pad_select("");
00461     R_pad_delete_item("cur_w");
00462 }
00463 
00473 void D_erase_window(void)
00474 {
00475     int t, b, l, r;
00476 
00477     D_get_screen_window(&t, &b, &l, &r);
00478     R_box_abs(l, t, r, b);
00479     R_flush();
00480 }
00481 
00482 void D_erase(const char *color)
00483 {
00484     int t, b, l, r;
00485     int colorindex;
00486 
00487     D_get_screen_window(&t, &b, &l, &r);
00488     D_clear_window();
00489 
00490     /* Parse and select background color */
00491     colorindex = D_parse_color(color, 0);
00492     D_raster_use_color(colorindex);
00493 
00494     /* Do the plotting */
00495     R_box_abs(l, t, r, b);
00496 
00497     /* Add erase item to the pad */
00498     D_set_erase_color(color);
00499 }
00500 
00501 void D_remove_windows(void)
00502 {
00503     char **pads;
00504     int npads;
00505     int i;
00506 
00507     R_pad_list(&pads, &npads);
00508 
00509     R_pad_select("");
00510     R_pad_delete_item("time");
00511     R_pad_delete_item("cur_w");
00512 
00513     for (i = 0; i < npads; i++) {
00514         R_pad_select(pads[i]);
00515         R_pad_delete();
00516     }
00517 }
00518 
00519 void D_full_screen(void)
00520 {
00521     D_remove_windows();
00522 
00523     D_new_window_percent("full_screen", 0.0, 100.0, 0.0, 100.0);
00524     if (D_set_cur_wind("full_screen") == 0)
00525         D_timestamp();
00526 
00527     R_standard_color(D_translate_color(DEFAULT_BG_COLOR));
00528     R_erase();
00529 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines