listboxc.c

来自「linux下的E_MAIL客户端源码」· C语言 代码 · 共 874 行 · 第 1/2 页

C
874
字号
	 (*tmpCalls[i])(&verifyData);      }      if ( !verifyData.ok ) return;//// Write the text back to the field in case it was changed, then make sure//   it's still ok to add it.//      if ( item.size() != oldSize ) {	 lb->textLen = item.size();	 XmTextFieldSetString(lb->itemText, item);	 lb->textUnique = !lb->items.includes(item);	 lb->UpdateButtons();	 if ( !lb->addEnabled ) {	    int	pos = lb->items.indexOf(item) + 1;	    lb->MakeVisible(pos);	    return;	 }      } // End if text size changed   } // End if text must be verified   lb->DeferCallbacks(True);//// Replace the selected item(s)//   XmString newString = XmStringCreateLtoR((char*)item, XmSTRING_DEFAULT_CHARSET);   XmListReplaceItemsPos(lb->itemList, &newString, posCount, positions[0]);   XmStringFree(newString);//// Replace the string//   int indx = positions[0] -1;   lb->items.insert(item, indx);   lb->items.remove(indx+1);//// remains selected//   XmListSelectPos(lb->itemList, positions[0], False);//// Make sure the item is visible//   lb->MakeVisible(positions[0]);   lb->textUnique = False;   lb->UpdateButtons();   lb->selectChange = lb->itemChange = True;   lb->DeferCallbacks(False);} // End ListBoxC DoItemReplace/*--------------------------------------------------------------- *  Callback routine to handle entry of text */voidListBoxC::DoTextChanged(Widget, ListBoxC *lb, XtPointer){   char		*str = XmTextFieldGetString(lb->itemText);   WXmString	text(str);   lb->textLen = strlen(str);   lb->textUnique = !XmListItemExists(lb->itemList, text);   lb->UpdateButtons();   XtFree(str);} // End ListBoxC DoTextChanged/*--------------------------------------------------------------- *  Callback routine to handle selection of list entries */voidListBoxC::DoSelect(Widget, ListBoxC *lb, XmListCallbackStruct *list){   lb->DeferCallbacks(True);   lb->selectCount = list->selected_item_count;   if(lb->selectCount == 1) {     char *labelItem;     XmStringGetLtoR(list->selected_items[0], XmFONTLIST_DEFAULT_TAG, &labelItem);     XmTextFieldSetString(lb->itemText, labelItem);     XtFree(labelItem);   } else {     XmTextFieldSetString(lb->itemText, "");   }   lb->textLen = 0;   lb->selectChange = True;   lb->UpdateButtons();   lb->DeferCallbacks(False);}/*--------------------------------------------------------------- *  Callback routine to handle double-click of list entry */voidListBoxC::DoItemOpen(Widget, ListBoxC *lb, XmListCallbackStruct *list){//// Copy text from selection to edit field//   WXmString	value(list->item);   char		*str = value;   XmTextFieldSetString(lb->itemText, str);   lb->textLen = strlen(str);   lb->textUnique = False;   XtFree(str);//// Undo the selection//   if ( XmListPosSelected(lb->itemList, list->item_position) ) {      XmListDeselectPos(lb->itemList, list->item_position);      lb->selectCount--;   } else {      XmListSelectPos(lb->itemList, list->item_position, False);      lb->selectCount++;   }   lb->UpdateButtons();} // End ListBoxC DoItemOpen/*--------------------------------------------------------------- *  Method to add a new item to the list */voidListBoxC::AddItem(StringC name){//// Check for duplicate name if necessary//   if ( !dupOk && items.includes(name) ) return;   DeferCallbacks(True);   items.add(name);   int pos;   if ( !sorted || items.size() == 1 )      pos = 0;          // append at bottom of list.   else      pos = items.indexOf(name) + 1;   WXmString	text((char*)name);   XmListAddItem(itemList, text, pos);   itemCount++;//// Disable the add button if this item matches text in the entry field//   char		*str = XmTextFieldGetString(itemText);   textUnique = (name != str);   XtFree(str);   itemChange = True;   UpdateButtons();   DeferCallbacks(False);} // End ListBoxC AddItem/*--------------------------------------------------------------- *  Method to add new items to the list */voidListBoxC::AddItems(const StringListC& list){   DeferCallbacks(True);   unsigned	count = list.size();   for (int i=0; i<count; i++) AddItem(*list[i]);   itemChange = True;   DeferCallbacks(False);}/*--------------------------------------------------------------- *  Method to set the items in the list */voidListBoxC::SetItems(const StringListC& names){   DeferCallbacks(True);   int	scount = selectCount;   int	icount = itemCount;   Clear();   int	count = names.size();   for (int i=0; i<count; i++) AddItem(*names[i]);   itemChange   = (count>0) && (icount>0);   selectChange = (scount>0);   DeferCallbacks(False);}/*--------------------------------------------------------------- *  Method to append the items in the list box to the given list */StringListC&ListBoxC::GetItems(StringListC& list){   list.SetSorted(sorted);   for (int i=0; i<itemCount; i++) list.add(*items[i]);   return(list);}/*--------------------------------------------------------------- *  Method to append the selected items in the list box to the given list */StringListC&ListBoxC::GetSelectedItems(StringListC& list){   list.SetSorted(sorted);//// Get the list of selected items//   XmStringTable	slist;   int			scount;   XtVaGetValues(itemList, XmNselectedItemCount, &scount,			   XmNselectedItems,     &slist, NULL);   StringC	item;   for (int i=0; i<scount; i++) {      char	*cs;      XmStringGetLtoR(slist[i], XmFONTLIST_DEFAULT_TAG, &cs);      item = cs;      list.add(item);      XtFree(cs);   }   return(list);} // End ListBoxC GetSelectedItems/*--------------------------------------------------------------- *  Method to remove an item from the list */voidListBoxC::RemoveItem(StringC name){//// Get position of item to be deleted//   int	index = items.indexOf(name);   if ( index == items.NULL_INDEX ) return;   DeferCallbacks(True);   selectChange = XmListPosSelected(itemList, index+1);   itemChange = True;//// Delete requested item//   XmListDeletePos(itemList, index+1);    items.remove(index);   itemCount--;//// Get number of selected items//   XtVaGetValues(itemList, XmNselectedItemCount, &selectCount, NULL);   UpdateButtons();   DeferCallbacks(False);} // End ListBoxC RemoveItem/*--------------------------------------------------------------- *  Method to remove items from the list */voidListBoxC::RemoveItems(const StringListC& list){   DeferCallbacks(True);   selectChange = False;//// Loop through items//   unsigned	count = list.size();   for (int i=0; i<count; i++) {      StringC	*name = list[i];      int	index = items.indexOf(*name);      if ( index != items.NULL_INDEX ) {	 selectChange = selectChange || XmListPosSelected(itemList, index+1);//// Delete requested item//	 XmListDeletePos(itemList, index+1); 	 items.remove(index);      }   }   itemChange = (count>0);   itemCount = items.size();//// Get number of selected items//   XtVaGetValues(itemList, XmNselectedItemCount, &selectCount, NULL);   UpdateButtons();   DeferCallbacks(False);} // End ListBoxC RemoveItem/*--------------------------------------------------------------- *  Callback routine to handle press of CLEAR */voidListBoxC::DoClear(Widget, ListBoxC *lb, XtPointer){//// Build verification dialog if necessary//   if ( !lb->clearWin ) {      WArgList	args;      args.AutoUnmanage(True);      if ( halApp->questionPM ) args.SymbolPixmap(halApp->questionPM);      lb->clearWin = XmCreateQuestionDialog(*lb, "clearWin", ARGS);      Widget	ok = XmMessageBoxGetChild(lb->clearWin, XmDIALOG_OK_BUTTON);      XtAddCallback(ok, XmNactivateCallback, (XtCallbackProc)FinishClear,                    (XtPointer)lb);   }   // unmanage the help button   XtUnmanageChild(XmMessageBoxGetChild(lb->clearWin, XmDIALOG_HELP_BUTTON));   XtManageChild(lb->clearWin);   XMapRaised(halApp->display, XtWindow(XtParent(lb->clearWin)));} // End ListBoxC DoClear/*--------------------------------------------------------------- *  Callback routine to handle press of OK in clear confirmation dialog */voidListBoxC::FinishClear(Widget, ListBoxC *lb, XtPointer){   lb->Clear();}/*--------------------------------------------------------------- *  Method to remove all items from the list */voidListBoxC::Clear(){   DeferCallbacks(True);   selectChange = (selectCount > 0);   itemChange   = (itemCount   > 0);   XmListDeleteAllItems(itemList);   items.removeAll();   itemCount   = 0;   selectCount = 0;   textUnique = True;   UpdateButtons();   DeferCallbacks(False);}/*--------------------------------------------------------------- *  Method to set sensitivities of buttons */voidListBoxC::UpdateButtons(){   addEnabled = textLen>0 && (dupOk || textUnique);   XtSetSensitive(addButton, addEnabled);   XtSetSensitive(remButton, selectCount>0);   XtSetSensitive(repButton, selectCount==1 && addEnabled);   XtSetSensitive(clrButton, itemCount>0);}/*--------------------------------------------------------------- *  Method to enable quick-help for components */voidListBoxC::HandleHelp(HalShellC *win, Help*){   win->HandleHelp(itemText);   win->HandleHelp(itemList);   win->HandleHelp(addButton);   win->HandleHelp(remButton);   win->HandleHelp(repButton);   win->HandleHelp(clrButton);}/*---------------------------------------------------------------------- *  Set a flag so that callbacks don't get called until all changes are *      complete. */voidListBoxC::DeferCallbacks(Boolean on){   if ( on ) {      deferCallbacks++;		// Up the count   } else if ( deferCallbacks > 0 ) {      deferCallbacks--;		// Lower the count      if ( deferCallbacks == 0 ) {	 if ( selectChange ) CallSelectChangeCallbacks();	 if ( itemChange   ) CallItemChangeCallbacks();	 selectChange =	 itemChange   = False;      }   }} // End VBoxC DeferCallbacks

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?