Widget Example

This example creates a widget with two buttons whose text strings change between two languages, depending on the selection from a drop-down list.

The following language catalogs are two separate files (as denoted by the <IDLffLangCat> tag for each) and should be placed on your system as such.

<?xml version="1.0"?> 
<!-- $Id: myButtonsText.eng.cat,v 1.1 2004 rsiDoc Exp $ --> 
<IDLffLangCat APPLICATION="myOpenButtons" VERSION="1.0" 
   AUTHOR="ITT"> 
   <LANGUAGE NAME="English"> 
      <KEY NAME="Button:OpenFile">Open File</KEY> 
      <KEY NAME="Button:OpenFolder">Open Folder</KEY> 
   </LANGUAGE> 
</IDLffLangCat> 
 
<?xml version="1.0"?> 
<!-- $Id: myButtonsText.fr.cat,v 1.1 2004 rsiDoc Exp $ --> 
<IDLffLangCat APPLICATION="myOpenButtons" VERSION="1.0" 
   AUTHOR="ITT"> 
   <LANGUAGE NAME="French"> 
      <KEY NAME="Button:OpenFile">Ouvrir le Fichier</KEY> 
      <KEY NAME="Button:OpenFolder">Ouvrir le Dossier</KEY> 
   </LANGUAGE> 
</IDLffLangCat> 

To use the following code, save it in a .pro file. You do not have to run it from the same directory containing the language catalog files.

; Routine to change the language of the button labels. 
PRO button_language_change, pstate 
   vLangString = (*pstate).vlang 
 
   ; Access the language catalog to retrieve string values. 
   oLangCat = OBJ_NEW( 'IDLffLangCat', vLangString, $ 
      APP_NAME='myOpenButtons' , APP_PATH=(*pstate).vpath) 
   ; Access and store language-specific strings in the structure. 
   strOpenFile = oLangCat->Query( 'Button:OpenFile' ) 
   strOpenFolder = oLangCat->Query( 'Button:OpenFolder' ) 
   WIDGET_CONTROL, (*pstate).pb1, SET_VALUE=strOpenFile 
   WIDGET_CONTROL, (*pstate).pb2, SET_VALUE=strOpenFolder 
END 
 
; Event handler for 'Open File' button. 
PRO button_file, event 
   sFile = DIALOG_PICKFILE( TITLE='Select image file' ) 
END 
 
; Event handler for 'Open Folder' button. 
PRO button_folder, event 
   sFolder = DIALOG_PICKFILE( /DIRECTORY, $ 
      TITLE='Choose the directory in which to store the data' ) 
END 
 
; Event handler for 'Language' droplist. 
PRO button_language_event, event 
   WIDGET_CONTROL, event.top, GET_UVALUE = pstate 
   ; Access user's language selection and store it in the pointer. 
   IF event.index EQ 0 THEN (*pstate).vlang = 'English' 
   IF event.index EQ 1 THEN (*pstate).vlang = 'French' 
   ; Call the procedure to change the button text. 
   button_language_change, pstate 
END 
 
; Widget-creation procedure 
PRO button_language 
   ; Prompt for path to catalog files 
   vpath=dialog_pickfile( TITLE='Select directory that ' + $ 
      'contains *.cat files', /DIRECTORY ) 
   IF vpath EQ '' THEN return 
 
   ; Create a top level base. Not specifying tab mode uses default 
   ; value of zero (do not allow widgets to receive or lose focus). 
   tlb = WIDGET_BASE( /COLUMN, TITLE = "Language Change", $ 
      XSIZE=220, /BASE_ALIGN_CENTER ) 
   ; Create the button widgets. 
   bbase = WIDGET_BASE( tlb, /COLUMN ) 
   pb1 = WIDGET_BUTTON( bbase, VALUE='Open File', $ 
      UVALUE='openFile', XSIZE=105, EVENT_PRO='button_file' ) 
   pb2 = WIDGET_BUTTON( bbase, VALUE='Open Folder', $ 
      UVALUE='openFolder', XSIZE=105, EVENT_PRO='button_folder' ) 
   ; Create a drop-down list indicating available catalogs. 
   vLangList = ['English', 'French'] 
   langDrop = WIDGET_DROPLIST( tlb, VALUE=vLangList, $
      TITLE='Language' ) 
   ; Draw the widgets and activate events. 
   WIDGET_CONTROL, tlb, /REALIZE 
 
   ; Create the state structure. 
   state = { $ 
      pb1:pb1, $ 
      pb2:pb2, $ 
      vlang:'', $ 
      vpath:vpath $ 
   } 
   pstate = PTR_NEW( state, /NO_COPY ) 
   WIDGET_CONTROL, tlb, SET_UVALUE=pstate 
   XMANAGER, 'button_language', tlb 
 
   ; Clean up pointers. 
   PTR_FREE, pstate 
END