Back to index

im-sdk  12.3.91
lookup_choice.c
Go to the documentation of this file.
00001 /*
00002   lookup_choice.c
00003 */
00004 
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include "iiimcfint.h"
00008 
00009 #define IIIMCF_LOOKUP_CHOICE_LABEL_INDEX_SIZE_MAX 32
00010 
00011 IIIMF_status
00012 iiimcf_lookup_choice_start(
00013     IIIMCF_context_rec *pc,
00014     IIIMP_message *pmes
00015 )
00016 {
00017     IIIMCF_lookup_choice_rec *pl = &pc->lookup_choice;
00018     IIIMP_lookup_choice_start_v *plcs = &pmes->v.lookup_choice_start;
00019 
00020     ASSERT(pmes->opcode == IM_LOOKUP_CHOICE_START);
00021 
00022     pl->masterp = plcs->master;
00023     pl->choices_per_window = plcs->choice_per_window;
00024     pl->rows = plcs->rows;
00025     pl->cols = plcs->columns;
00026     pl->direction = plcs->direction;
00027     pl->label_owner = plcs->label_owner;
00028 
00029     pl->enabled_range_start = pl->enabled_range_end = 0;
00030 
00031     IIIMCF_SET_STATE(pc, IIIMCF_CONTEXT_LOOKUP_CHOICE_ENABLED);
00032     IIIMCF_SET_STATE_CHANGE(pc, IIIMCF_STATE_LOOKUP_CHOICE_CHANGED);
00033     
00034     return iiimcf_store_simple_event(pc, IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_START);
00035 }
00036 
00037 IIIMF_status
00038 iiimcf_lookup_choice_done(
00039     IIIMCF_context_rec *pc
00040 )
00041 {
00042     if (IIIMCF_IS_ENABLED(pc, IIIMCF_CONTEXT_LOOKUP_CHOICE_ENABLED)) {
00043        IIIMCF_RESET_STATE(pc, IIIMCF_CONTEXT_LOOKUP_CHOICE_ENABLED);
00044        IIIMCF_SET_STATE_CHANGE(pc, IIIMCF_STATE_LOOKUP_CHOICE_CHANGED);
00045        return iiimcf_store_simple_event(pc, IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_DONE);
00046     }
00047 
00048     return IIIMF_STATUS_SUCCESS;
00049 }
00050 
00051 IIIMF_status
00052 iiimcf_update_lookup_choice(
00053     IIIMCF_context_rec *pc,
00054     IIIMP_message *pmes
00055 )
00056 {
00057     int i, nc, nl;
00058     int flag, *pflags;
00059     IIIMF_status st;
00060     IIIMP_text *pimt;
00061     IIIMCF_text_rec *ptr;
00062     IIIMCF_lookup_choice_rec *pl = &pc->lookup_choice;
00063     IIIMP_lookup_choice_draw_v *plcd = &pmes->v.lookup_choice_draw;
00064     ASSERT(pmes->opcode == IM_LOOKUP_CHOICE_DRAW);
00065 
00066     st = iiimcf_convert_iiimp_text_to_text(plcd->title, &pl->title);
00067     if (st != IIIMF_STATUS_SUCCESS) return st;
00068     for (nc = 0, pimt = plcd->choice; pimt; pimt = pimt->next) nc++;
00069 
00070     if (nc > pl->size) {
00071        int osize = pl->size;
00072        int inc = nc - osize;
00073 
00074        ptr = (IIIMCF_text_rec*) realloc(pl->pcandidates, sizeof(*ptr) * nc);
00075        if (!ptr) return IIIMF_STATUS_MALLOC;
00076        memset(ptr + osize, 0, sizeof(*ptr) * inc);
00077        pl->pcandidates = ptr;
00078        ptr = (IIIMCF_text_rec*) realloc(pl->plabels, sizeof(*ptr) * nc);
00079        if (!ptr) return IIIMF_STATUS_MALLOC;
00080        memset(ptr + osize, 0, sizeof(*ptr) * inc);
00081        pl->plabels = ptr;
00082        pflags = (int*) realloc(pl->pflags, sizeof(*pflags) * nc);
00083        if (!pflags) return IIIMF_STATUS_MALLOC;
00084        memset(pflags + osize, 0, sizeof(*pflags) * inc);
00085        pl->pflags = pflags;
00086     }
00087     for (i = plcd->first, ptr = pl->pcandidates, pflags = pl->pflags, pimt = plcd->choice;
00088         pimt; i++, ptr++, pflags++, pimt = pimt->next) {
00089        st = iiimcf_convert_iiimp_text_to_text(pimt, ptr);
00090        if (st != IIIMF_STATUS_SUCCESS) return st;
00091        flag = IIIMCF_LOOKUP_CHOICE_ITEM_ENABLED;
00092        if (i == plcd->current)
00093            flag |= IIIMCF_LOOKUP_CHOICE_SELECTED_ITEM;
00094        *pflags = flag;
00095     }
00096     for (nl = 0, pimt = plcd->index_label; pimt; pimt = pimt->next) nl++;
00097     if ((pl->masterp != IM_LOOKUP_CHOICE_START_SERVER_IS_MASTER)
00098        || (nc != nl)) {
00099        /* make label */
00100        int endi = plcd->first + nc;
00101 
00102        for (i = plcd->first, ptr = pl->plabels;
00103             i < endi; i++, ptr++) {
00104            unsigned char stridx[IIIMCF_LOOKUP_CHOICE_LABEL_INDEX_SIZE_MAX];
00105            snprintf(stridx, sizeof(stridx), "%d:", i);
00106            st = iiimcf_construct_text_from_UTF8(stridx, ptr);
00107        }
00108     } else {
00109        for (ptr = pl->plabels, pimt = plcd->index_label;
00110             pimt; pimt = pimt->next, ptr++) {
00111            st = iiimcf_convert_iiimp_text_to_text(pimt, ptr);
00112            if (st != IIIMF_STATUS_SUCCESS) return st;
00113        }
00114     }
00115     pl->idx_first = plcd->first;
00116     pl->idx_last = plcd->last;
00117     pl->idx_current = plcd->current;
00118     pl->enabled_range_start = 0;
00119     if (pl->masterp == IM_LOOKUP_CHOICE_START_CLIENT_IS_MASTER) {
00120        if (nc > pl->choices_per_window)
00121            pl->enabled_range_end = pl->choices_per_window;
00122        else
00123            pl->enabled_range_end = nc;
00124     } else {
00125        pl->enabled_range_end = nc;
00126     }
00127     pl->size = nc;
00128 
00129     IIIMCF_SET_STATE_CHANGE(pc, IIIMCF_STATE_LOOKUP_CHOICE_CHANGED);
00130 
00131     return iiimcf_store_simple_event(pc, IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_CHANGE);
00132 }
00133 
00134 IIIMF_status
00135 iiimcf_process_lookup_choice(
00136     IIIMCF_context_rec *pc,
00137     IIIMP_message *pmes
00138 )
00139 {
00140     IIIMCF_lookup_choice_rec *pl = &pc->lookup_choice;
00141     IIIMP_lookup_choice_process_v *plcp = &pmes->v.lookup_choice_process;
00142     ASSERT(pmes->opcode == IM_LOOKUP_CHOICE_PROCESS);
00143 
00144     if (plcp->type == IM_LOOKUP_CHOICE_PROCESS_INDEX) {
00145        int ridx, ridx_o;
00146 
00147        if (!((plcp->value >= pl->idx_first) || (plcp->value <= pl->idx_last)))
00148            return IIIMF_STATUS_SUCCESS;  /* Invalid index.  Simply ignore it. */
00149        ridx = plcp->value - pl->idx_first;
00150        if (ridx >= pl->size) 
00151            return IIIMF_STATUS_SUCCESS;  /* Invalid index.  Simply ignore it. */
00152        ridx_o = pl->idx_current - pl->idx_first;
00153        if ((ridx_o >= 0) && (ridx_o < pl->size)) {
00154            pl->pflags[ridx_o] &= ~IIIMCF_LOOKUP_CHOICE_SELECTED_ITEM;
00155        }
00156        pl->pflags[ridx] |= IIIMCF_LOOKUP_CHOICE_SELECTED_ITEM;
00157        pl->idx_current = ridx;
00158     } else if (plcp->type == IM_LOOKUP_CHOICE_PROCESS_PAGE) {
00159        int st = pl->enabled_range_start;
00160        int end = pl->enabled_range_end;
00161        switch (plcp->value) {
00162          case IM_LOOKUP_CHOICE_PROCESS_PAGE_NEXT_PAGE:
00163           st += pl->choices_per_window;
00164           if (st >= pl->size) return IIIMF_STATUS_SUCCESS; /* ignore */
00165           end += pl->choices_per_window;
00166           break;
00167          case IM_LOOKUP_CHOICE_PROCESS_PAGE_PREV_PAGE:
00168           st -= pl->choices_per_window;
00169           end -= pl->choices_per_window;
00170           if (end <= 0) return IIIMF_STATUS_SUCCESS; /* ignore */
00171           break;
00172          case IM_LOOKUP_CHOICE_PROCESS_PAGE_FIRST_PAGE:
00173           st = 0;
00174           end = pl->choices_per_window;
00175           break;
00176          case IM_LOOKUP_CHOICE_PROCESS_PAGE_LAST_PAGE:
00177           st = pl->size - pl->choices_per_window;
00178           end = pl->size;
00179           break;
00180          default:
00181           return IIIMF_STATUS_SUCCESS;
00182        }
00183 
00184        if (st < 0) st = 0;
00185        else if (st >= pl->size) st = pl->size - 1;
00186 
00187        if (end < 0) end = 0;
00188        else if (end < st) end = st;
00189        else if (end > pl->size) end = pl->size;
00190 
00191        pl->enabled_range_start = st;
00192        pl->enabled_range_end = end;
00193     } else {
00194        return IIIMF_STATUS_SUCCESS;
00195     }
00196     
00197     IIIMCF_SET_STATE_CHANGE(pc, IIIMCF_STATE_LOOKUP_CHOICE_CHANGED);
00198 
00199     return iiimcf_store_simple_event(pc, IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_CHANGE);
00200 }
00201 
00202 IIIMF_status
00203 iiimcf_get_lookup_choice_size(
00204     IIIMCF_lookup_choice lookup_choice,
00205     int *psize,
00206     int *pindex_of_first_candidate,
00207     int *pindex_of_last_candidate,
00208     int *pindex_of_current_candidate
00209 )
00210 {
00211     IIIMCF_lookup_choice_rec *pl = (IIIMCF_lookup_choice_rec*) lookup_choice;
00212 
00213     *psize = pl->enabled_range_end - pl->enabled_range_start;
00214     *pindex_of_first_candidate = pl->idx_first;
00215     *pindex_of_last_candidate = pl->idx_last;
00216     *pindex_of_current_candidate = pl->idx_current;
00217 
00218     return IIIMF_STATUS_SUCCESS;
00219 }
00220 
00221 IIIMF_status
00222 iiimcf_get_lookup_choice_configuration(
00223     IIIMCF_lookup_choice lookup_choice,
00224     int *pchoices_per_window,
00225     int *pnumber_of_rows,
00226     int *pnumber_of_columns,
00227     int *pdirection
00228 )
00229 {
00230     IIIMCF_lookup_choice_rec *pl = (IIIMCF_lookup_choice_rec*) lookup_choice;
00231 
00232     *pchoices_per_window = pl->choices_per_window;
00233     *pnumber_of_rows = pl->rows;
00234     *pnumber_of_columns = pl->cols;
00235     *pdirection = pl->direction;
00236 
00237     return IIIMF_STATUS_SUCCESS;
00238 }
00239 
00240 IIIMF_status
00241 iiimcf_get_lookup_choice_item(
00242     IIIMCF_lookup_choice lookup_choice,
00243     int idx,
00244     IIIMCF_text *pcandidate,
00245     IIIMCF_text *plabel,
00246     int *pflag
00247 )
00248 {
00249     IIIMCF_lookup_choice_rec *pl = (IIIMCF_lookup_choice_rec*) lookup_choice;
00250 
00251     if (idx < 0) return IIIMF_STATUS_ARGUMENT;
00252     idx += pl->enabled_range_start;
00253     if (idx >= pl->enabled_range_end) return IIIMF_STATUS_ARGUMENT;
00254 
00255     *pcandidate = pl->pcandidates + idx;
00256     *plabel = pl->plabels + idx;
00257     *pflag = pl->pflags[idx];
00258 
00259     return IIIMF_STATUS_SUCCESS;
00260 }
00261 
00262 IIIMF_status
00263 iiimcf_get_lookup_choice_title(
00264     IIIMCF_lookup_choice lookup_choice,
00265     IIIMCF_text *ptitle
00266 )
00267 {
00268     IIIMCF_lookup_choice_rec *pl = (IIIMCF_lookup_choice_rec*) lookup_choice;
00269 
00270     *ptitle = &pl->title;
00271 
00272     return IIIMF_STATUS_SUCCESS;
00273 }
00274 
00275 IIIMF_status
00276 iiimcf_get_lookup_choice(
00277     IIIMCF_context context,
00278     IIIMCF_lookup_choice *plookup_choice
00279 )
00280 {
00281     IIIMCF_context_rec *pc = (IIIMCF_context_rec*) context;
00282 
00283     if (IIIMCF_IS_IC_INVALID(pc)) return IIIMF_STATUS_IC_INVALID;
00284     if ((!IIIMCF_IS_ENABLED(pc, IIIMCF_CONTEXT_LOOKUP_CHOICE_ENABLED))
00285        || (!IIIMCF_IS_VALID_LOOKUP_CHOICE(&pc->lookup_choice)))
00286        return IIIMF_STATUS_NO_LOOKUP_CHOICE;
00287 
00288     *plookup_choice = &pc->lookup_choice;
00289 
00290     return IIIMF_STATUS_SUCCESS;
00291 }
00292 
00293 void
00294 iiimcf_destruct_lookup_choice(
00295     IIIMCF_lookup_choice_rec *pl
00296 )
00297 {
00298     int i;
00299     IIIMCF_text_rec *plabels, *pcands;
00300 
00301     iiimcf_destruct_text(&pl->title);
00302     plabels = pl->plabels;
00303     pcands = pl->pcandidates;
00304     for (i = 0; i < pl->size; i++, plabels++, pcands++) {
00305        iiimcf_destruct_text(plabels);
00306        iiimcf_destruct_text(pcands);
00307     }
00308     free(pl->plabels);
00309     free(pl->pcandidates);
00310 }
00311 
00312 /* Local Variables: */
00313 /* c-file-style: "iiim-project" */
00314 /* End: */