Thursday, March 10, 2016

MFC tips

Working with MFC ListCtrl


LV_ITEM lvi -- this is the structure you need to fill out to add an entry into the list.
lvi has two items that may be confusing to you, nItem and nSubItem. Think of these two as
row and column. nItem is the row and nSubItem is the column of that row.

Here is a sample code to add an entry to the CListCtrl:


        LV_ITEM lvi;
        CString sAlias(StringManipulationUtil::NQStringToCString(*iter));
        ZeroMemory(&lvi, sizeof(LV_ITEM));
        lvi.mask = LVIF_TEXT; //can be an image , text or etc.
        lvi.iItem = m_ctlList.GetItemCount(); //insert at the bottom
        lvi.pszText = (LPTSTR) (const TCHAR *) (sAlias); //text to show
        lvi.cchTextMax    = sAlias.GetLength()+ 1; //size of text
        m_ctlList.InsertItem(&lvi);

How to add your own accelerators in MFC?

Accelerators are keyboard shortcuts that will trigger some events.
For example, when you click on a button, onClicked event will be trigger for that button.
There are two information associated with an event: the event type (onClicked) and the ID of the control.
A regular message map would be the followings:

BEGIN_MESSAGE_MAP(SAATAPPDlgConsistencyCheck, CDlgHelper)
    ON_BN_CLICKED(IDC_BTN_GOTO, OnBtnGoto)
    ...
END_MESSAGE_MAP()


Notice the event type defined by the macro ON_BN_CLICKED takes in the id of the control, and the handler.

How do you capture this event with keyboard shortcuts?
First, you need to create an accelerator. You can do this by opening the edit.rc file and under accelerator add a new accelerator table. Here is the example with screenshots:

Creating a new accelerator node:

 








Add new shortcuts to existing accelerator node:
1. double click on an existing accelerator node



 2. Here is a list of shortcuts in the accelerator node IDR_NAVIGATION
This table contain the control ids, and the key board shortcuts assigned to them.
When the keyboard shortcut is executed by the users, the control having the id will receive an oncommand event.






3. To create a new keyboard shortcut, right click and select new accelerator



 4. Once a new item is created, you can select and right click the new item to assign a new keyboard shortcut for it.



Notice each shortcut has an ID which can be the ID of a control. For example, IDC_BTN_UP in the screenshot is the id of a button control. The keyboard shortcut will trigger an event similar to a mouse click on the button control. You can change the ID. Just select the id and a drop down box will be displayed. From the drop down box, you can select any of the control ID defined for the application.




In order for the keyboard shortcut to work, you need to apply/load the custom accelerator node/table to the dialog where you want to handle the shortcuts or containing the desired control(s).
The one accelerator node that will always exist or the default one loaded for the program is the IDR_MAINFRAME.

If you create you own accelerator table/node, you need to load it.
You can load the accelerator table when the dialog, containing the desired controls, is initialized.

BOOL CShtHelper::OnInitDialog()
{
    if(m_bNeedsStatusBar)
    {   
        CreateStatusBar(this);
    }
    BOOL bResult = CResizeableSheet::OnInitDialog();
    //load the accelerator table you created (IDR_NAVIGATION).
    m_hAccel = ::LoadAccelerators(AfxGetResourceHandle(),
                                                          MAKEINTRESOURCE(IDR_NAVIGATION));
...
}

If the shortcut or control is being used in many places, you may want to prevent the same shortcut being propagate to many places. By overwriting the PreTranslateMessage method, you can prevent the message from being propagated:

BOOL CShtHelper::PreTranslateMessage(MSG* pMsg)
{
//if this class/dialog can translate the message (keyboard shortcut), then don't propagate the message;
//otherwise, forward the message to the parent.
//Translate the message using your accelerator table, this will trigger the on command on the some control if
//the message (key pressed) matches an entry in the table.
    if ( !(m_hAccel && ::TranslateAccelerator(m_hWnd, m_hAccel, pMsg)) && !m_tabCtrl.TranslatePropSheetMsg(pMsg))
    {
        return CResizeableSheet::PreTranslateMessage(pMsg);
    }

    return TRUE;
}

Finally, you need to register the message map to tell the program what to do when the button is clicked.
Here we want the OnUp method to be called when the button IDC_BTN_UP is clicked.

BEGIN_MESSAGE_MAP(CDlgInitBlock2, CInitBlockSubDialog)
    ON_CBN_SELCHANGE(IDC_SOURCE_TYPE_COMBO, OnComboTypeChange)
    ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelectionChanged)
    ON_BN_CLICKED(IDC_BTN_UP, OnUp)
    ON_BN_CLICKED(IDC_BTN_DOWN, OnDown)
    ON_BN_CLICKED(IDC_BTN_Delete, OnDelete)
END_MESSAGE_MAP()


How to find the child window having a given name?

If the child window is part of a MDI application, calling FindWindowEx is not enough.
FindWindowEx(AfxGetMainWnd, 0 ,0, windowTitle); //not working because this is a mdi application
FindWindowEx(MDIClientWnd, 0,0,WindowTitle); //not working
FindWindowEx(MDIClientWnd,0,registerClassWndName, WindowTitle); //working, so you need to register it.

Wednesday, July 15, 2015

Basic Yum Commands

Basic Yum Commands and how to use them

All the information here is taken from this page for my reference: http://yum.baseurl.org/wiki/YumCommands

This is not an exhaustive list of all yum commands but it is a list of the basic/common/important ones. For a complete list see the yum man page.
   yum list [available|installed|extras|updates|obsoletes|all|recent] [pkgspec] 
This command lets you list packages in any repository enabled on your system or installed. It also lets you list specific types of packages as well as refine your list with a package specification of any of the package's name, arch, version, release, epoch.

   yum list
By default 'yum list' without any options will list all packages in all the repositories and all the packages installed on your system. Note: 'yum list all' and 'yum list' give the same output.
   yum list available
Lists all the packages available to be installed in any enabled repository on your system.
   yum list installed
This is equivalent to rpm -qa. It lists all the packages installed on the system.
   yum list extras
This command lists any installed package which no longer appears in any of your enabled repositories. Useful for finding packages which linger between upgrades or things installed not from a repo.
   yum list obsoletes
This command lists any obsoleting relationships between any available package and any installed package.
   yum list updates
This command lists any package in an enabled repository which is an update for any installed package.
   yum list recent
This command lists any package added to any enabled repository in the last seven(7) days.
   yum list pkgspec
This command allows you to refine your listing for particular packages.

Examples of pkgspecs:
      yum list zsh 
      yum list joe\* 
      yum list \*.i386 
      yum list dovecot-1.0.15 

   yum install/remove/update
....

   yum check-update
Exactly like yum list updates but returns an exit code of 100 if there are updates available. Handy for shell scripting.

   yum grouplist
   yum groupinfo
   yum groupinstall
   yum groupupdate
   yum groupremove
Please see the YumGroups page on this wiki for information about the above commands.

   yum info
This displays more information about any package installed or available. It takes the same arguments as yum list but it is best run with a specific package name or glob. Example:
     $ yum info yum
     Installed Packages
     Name       : yum
     Arch       : noarch
     Version    : 3.2.20
     Release    : 3.fc10
     Size       : 2.5 M
     Repo       : installed
     Summary    : RPM installer/updater
     URL        : http://yum.baseurl.org/
     License    : GPLv2+
     Description: Yum is a utility that can check for and automatically download and
                : install updated RPM packages. Dependencies are obtained and downloaded
                : automatically prompting the user as necessary.

  yum search
This allows you to search for information from the various metadata available about packages. It can accept multiple arguments. It will output the packages which match the most terms first followed by the next highest number of matches, etc. Specifically yum search looks at the following fields: name, summary, description, url. If you're searching for what package provides a certain command try yum provides instead.
Search example:
$ yum search python rsync ssh
========================= Matched: python, rsync, ssh ==========================
rdiff-backup.i386 : Convenient and transparent local/remote incremental
                  : mirror/backup

============================ Matched: python, rsync ============================
cobbler.noarch : Boot server configurator

============================= Matched: python, ssh =============================
denyhosts.noarch : A script to help thwart ssh server attacks
pexpect.noarch : Pure Python Expect-like module
python-paramiko.noarch : A SSH2 protocol library for python
python-twisted-conch.i386 : Twisted SSHv2 implementation

============================= Matched: rsync, ssh ==============================
duplicity.i386 : Encrypted bandwidth-efficient backup using rsync algorithm
pssh.noarch : Parallel SSH tools
   yum provides/yum whatprovides
This command searches for which packages provide the requested dependency of file. This also takes wildcards for files. Examples:
$ yum provides MTA
2:postfix-2.5.5-1.fc10.i386 : Postfix Mail Transport Agent
Matched from:
Other       : MTA

exim-4.69-7.fc10.i386 : The exim mail transfer agent
Matched from:
Other       : MTA

sendmail-8.14.3-1.fc10.i386 : A widely used Mail Transport Agent (MTA)
Matched from:
Other       : Provides-match: MTA


$ yum provides \*bin/ls
coreutils-6.12-17.fc10.i386 : The GNU core utilities: a set of tools commonly
                            : used in shell scripts
Matched from:
Filename    : /bin/ls

   yum shell
....

   yum makecache
Is used to download and make usable all the metadata for the currently enabled yum repos. This is useful if you want to make sure the cache is fully current with all metadata before continuing.

   yum clean
During its normal use yum creates a cache of metadata and packages. This cache can take up a lot of space. The yum clean command allows you to clean up these files. All the files yum clean will act on are normally stored in /var/cache/yum.
Example commands and what they do:

        yum clean packages
This cleans up any cached packages in any enabled repository cache directory.

        yum clean metadata
This cleans up any xml metadata that may have been cached from any enabled repository.
        yum clean dbcache
Yum will create or download some sqlite database files as part of its normal operation. This command clean up the cached copies of those from any enabled repository cache.
        yum clean all
Clean all cached files from any enabled repository. Useful to run from time to time to make sure there is nothing using unnecessary space.

If you cannot install certain extra packages, it means your current Yum repositories do not have them. You can add another repository accessible to Yum. Just download the repo file and put it under /etc/yum.repo.d/. To find the repository that would contain your package, just google for "packagename yum repository". Usually there should be a link to download the repo file.

Run  >yum listrepo     will list all the repositories installed on your system.

To find out which repository contain the package you want to install, you can run
>yum list available | grep packagename

Example of downloading a yum repo:
wget -O /etc/yum.repos.d/bigtop.repo http://archive.apache.org/dist/bigtop/bigtop-0.5.0/repos/[centos5|centos6|fedora17]/bigtop.repo