listboxc.c
来自「linux下的E_MAIL客户端源码」· C语言 代码 · 共 874 行 · 第 1/2 页
C
874 行
/* * $Id: ListBoxC.C,v 1.2 2000/05/07 12:26:10 fnevgeny Exp $ * * Copyright (c) 1992 HaL Computer Systems, Inc. All rights reserved. * * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. * 1315 Dell Avenue * Campbell, CA 95008 * * Author: Greg Hilton * Contributors: Tom Lang, Frank Bieser, and others * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * http://www.gnu.org/copyleft/gpl.html * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include <config.h>#include "HalAppC.h"#include "ListBoxC.h"#include "WXmString.h"#include "StringListC.h"#include "rsrc.h"#include "HalShellC.h"#include "TextMisc.h"#include <Xm/TextF.h>#include <Xm/List.h>#include <Xm/Form.h>#include <Xm/Frame.h>#include <Xm/RowColumn.h>#include <Xm/PushB.h>#include <Xm/ScrolledW.h>#include <Xm/MessageB.h>/*--------------------------------------------------------------- * Constructor */ListBoxC::ListBoxC(Widget parent, const char *name, ArgList argv, Cardinal argc){ WArgList args; itemCount = 0; addEnabled = False; selectCount = 0; textLen = 0; textUnique = True; removeWin = NULL; clearWin = NULL; deferCallbacks = 0;//// Create widget and get resources// mainForm = XmCreateForm(parent, (char *)name, argv, argc); sorted = get_boolean("ListBoxC", mainForm, "sorted", False); dupOk = get_boolean("ListBoxC", mainForm, "allowDuplicates", True); items.AllowDuplicates(dupOk); items.SetSorted(sorted);/*--------------------------------------------------------------- * Create mainForm hierarchy * * mainForm * ScrolledList itemList * TextField itemText * Frame buttonFrame * RowColumn buttonBox */ args.Reset(); args.TopAttachment(XmATTACH_NONE); args.BottomAttachment(XmATTACH_FORM); args.LeftAttachment(XmATTACH_FORM); args.RightAttachment(XmATTACH_FORM); args.MarginWidth(0); args.MarginHeight(0); args.ShadowThickness(0); Widget buttonFrame = XmCreateFrame(mainForm, "buttonFrame", ARGS); args.Reset(); args.ChildType(XmFRAME_TITLE_CHILD); args.ChildHorizontalAlignment(XmALIGNMENT_CENTER); args.Packing(XmPACK_COLUMN); args.Orientation(XmHORIZONTAL); args.EntryAlignment(XmALIGNMENT_CENTER); buttonBox = XmCreateRowColumn(buttonFrame, "buttonBox", ARGS); args.Reset(); args.TopAttachment(XmATTACH_NONE); args.BottomAttachment(XmATTACH_WIDGET, buttonFrame); args.LeftAttachment(XmATTACH_FORM); args.RightAttachment(XmATTACH_FORM); itemText = CreateTextField(mainForm, "itemText", ARGS); XtAddCallback(itemText, XmNvalueChangedCallback, (XtCallbackProc)DoTextChanged, (XtPointer)this); XtAddCallback(itemText, XmNactivateCallback, (XtCallbackProc)DoTextReturn, (XtPointer)this); args.Reset(); args.TopAttachment(XmATTACH_FORM); args.BottomAttachment(XmATTACH_WIDGET, itemText); args.LeftAttachment(XmATTACH_FORM); args.RightAttachment(XmATTACH_FORM); args.SelectionPolicy(XmMULTIPLE_SELECT); args.ListSizePolicy(XmCONSTANT); itemList = XmCreateScrolledList(mainForm, "itemList", ARGS); itemWin = XtParent(itemList); XtAddCallback(itemList, XmNmultipleSelectionCallback, (XtCallbackProc)DoSelect, (XtPointer)this); XtAddCallback(itemList, XmNdefaultActionCallback, (XtCallbackProc)DoItemOpen, (XtPointer)this);/*--------------------------------------------------------------- * Create buttonBox hierarchy * * buttonBox * PushButton addButton * PushButton remButton * PushButton repButton * PushButton clrButton */ addButton = XmCreatePushButton(buttonBox, "addButton", 0,0); remButton = XmCreatePushButton(buttonBox, "remButton", 0,0); repButton = XmCreatePushButton(buttonBox, "repButton", 0,0); clrButton = XmCreatePushButton(buttonBox, "clrButton", 0,0); XtAddCallback(addButton, XmNactivateCallback, (XtCallbackProc)DoItemAdd, (XtPointer)this); XtAddCallback(remButton, XmNactivateCallback, (XtCallbackProc)DoRemove, (XtPointer)this); XtAddCallback(repButton, XmNactivateCallback, (XtCallbackProc)DoItemReplace, (XtPointer)this); XtAddCallback(clrButton, XmNactivateCallback, (XtCallbackProc)DoClear, (XtPointer)this); UpdateButtons();//// Manage children// Widget list[4]; list[0] = addButton; list[1] = remButton; list[2] = repButton; list[3] = clrButton; XtManageChildren(list, 4); // buttonBox children XtManageChild(buttonBox); // buttonFrame children XtManageChild(itemList); // itemWin children list[0] = itemText; list[1] = buttonFrame; XtManageChildren(list, 2); // mainForm children} // End ListBoxC Constructor/*--------------------------------------------------------------- * Callback routine to handle CR in text field */voidListBoxC::DoTextReturn(Widget, ListBoxC *lb, XtPointer){ lb->AddItem((Boolean)True /*Clear*/);}/*--------------------------------------------------------------- * Callback routine to handle press of ADD */voidListBoxC::DoItemAdd(Widget, ListBoxC *lb, XtPointer){ lb->AddItem((Boolean)True /*Clear*/);}/*--------------------------------------------------------------- * Method to add item in text field to list */voidListBoxC::AddItem(Boolean clear){//// If clear is true, this was called because the return key was hit in the// field. Only continue if adds are allowed// if ( !addEnabled ) return; // Keeps duplicates out//// Read text from field and add to list// char *str = XmTextFieldGetString(itemText); StringC item(str); XtFree(str);//// Call verify callbacks if necessary// if ( verifyCalls.size() > 0 ) { int oldSize = item.size(); ListBoxVerifyT verifyData; verifyData.listBox = this; verifyData.text = &item; verifyData.ok = True; CallbackListC tmpCalls = verifyCalls; // In case a callback removes // itself unsigned count = verifyCalls.size(); for (int i=0; i<count && verifyData.ok; i++) { (*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 ) { textLen = item.size(); XmTextFieldSetString(itemText, item); textUnique = !items.includes(item); UpdateButtons(); if ( !addEnabled ) { int pos = items.indexOf(item) + 1; MakeVisible(pos); return; } } // End if text size changed } // End if text must be verified int *posList, posCount; XmListGetSelectedPos(itemList, &posList, &posCount); int pos; if ( posCount == 0 ) { items.add(item); if ( !sorted || items.size() == 1 ) pos = 0; else pos = items.indexOf(item) + 1; } else { pos = posList[posCount-1]; items.insert(item, pos-1); } WXmString wstr((char *)item); XmListAddItem(itemList, wstr, pos);// if (items.size() == 1)// XmListSetKbdItemPos(itemList, 1); itemCount++;//// Clear the text if necessary// if ( clear ) { XmTextFieldSetString(itemText, ""); textLen = 0; textUnique = True; } DeferCallbacks(True);//// Make the item visible// MakeVisible(pos); itemChange = True; UpdateButtons(); DeferCallbacks(False);} // End ListBoxC AddItem/*--------------------------------------------------------------- * Method to make the specified item visible */voidListBoxC::MakeVisible(int pos){ int topPos, botPos, visCount; XtVaGetValues(itemList, XmNtopItemPosition, &topPos, XmNvisibleItemCount, &visCount, NULL); botPos = topPos + visCount - 1; if ( pos < topPos ) XmListSetPos (itemList, pos); else if ( pos > botPos ) XmListSetBottomPos(itemList, pos);}/*--------------------------------------------------------------- * Callback routine to handle press of REMOVE */voidListBoxC::DoRemove(Widget, ListBoxC *lb, XtPointer){//// Build verification dialog if necessary// if ( !lb->removeWin ) { WArgList args; args.AutoUnmanage(True); if ( halApp->questionPM ) args.SymbolPixmap(halApp->questionPM); lb->removeWin = XmCreateQuestionDialog(*lb, "removeWin", ARGS); Widget ok = XmMessageBoxGetChild(lb->removeWin, XmDIALOG_OK_BUTTON); XtAddCallback(ok, XmNactivateCallback, (XtCallbackProc)FinishRemove, (XtPointer)lb); } // unmanage the help button XtUnmanageChild(XmMessageBoxGetChild(lb->removeWin, XmDIALOG_HELP_BUTTON)); XtManageChild(lb->removeWin); XMapRaised(halApp->display, XtWindow(XtParent(lb->removeWin)));} // End ListBoxC DoRemove/*--------------------------------------------------------------- * Callback routine to handle press of OK in remove confirmation dialog */voidListBoxC::FinishRemove(Widget, ListBoxC *lb, XtPointer){ lb->RemoveSelected();}/*--------------------------------------------------------------- * Method to remove all selected items */voidListBoxC::RemoveSelected(){//// Get selected items and remove them// int *posList, posCount, count; XmListGetSelectedPos(itemList, &posList, &count); if(count) { DeferCallbacks(True); selectChange = False; // // Loop through items // for (int i=0; i<count; i++) { XmListGetSelectedPos(itemList, &posList, &posCount); int index = posList[0]; // // Delete requested item (which are moving around on us) // XmListDeletePos(itemList, index); items.remove(index-1); } itemChange = (count>0); itemCount = items.size(); // // Get number of selected items // XtVaGetValues(itemList, XmNselectedItemCount, &selectCount, NULL); UpdateButtons(); DeferCallbacks(False); }} // End ListBoxC RemoveSelected/*--------------------------------------------------------------- * Callback routine to handle press of REPLACE */voidListBoxC::DoItemReplace(Widget, ListBoxC *lb, XtPointer){//// Get position of selected item// int posCount; int *positions; XmListGetSelectedPos(lb->itemList, &positions, &posCount); //// Get string to be added// char *cs = XmTextFieldGetString(lb->itemText); StringC item(cs); XtFree(cs);//// Call verify callbacks if necessary// if ( lb->verifyCalls.size() > 0 ) { int oldSize = item.size(); ListBoxVerifyT verifyData; verifyData.listBox = lb; verifyData.text = &item; verifyData.ok = True; CallbackListC tmpCalls = lb->verifyCalls; // In case a callback // removes itself unsigned count = lb->verifyCalls.size(); for (int i=0; i<count && verifyData.ok; i++) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?