A Developer's Diary

Apr 3, 2010

Visual Studio - Auto formatting your code

Whenever you paste any code from other applications into a source file within Visual Studio, the formatting gets messed up. You can use the following tip to format the code if written in C++, C# and VB

Steps

1. Select the text portion to format OR (Ctrl + a)
2. Hold Ctrl and hit k followed by f (Ctrl + k + f)


Read more ...

Apr 1, 2010

Internet Explorer vs Firefox Keystroke detection

The DOM Level 2 Event specification does not provide a key event module. An event module designed for use with keyboard input devices will be included in a later version of the DOM specification.

With the lack of a specification and browser vendors coming out with their own implementation of Key Events, the compatibility issues between the browsers is bound to appear


#1. Code for handling keyboard event in Internet Explorer
<html>
<head>
<script type="text/javascript">

function keyDownListener()
{
    var key;
    key = window.event.keyCode;
    alert('Key Code ' + key);
    document.keyForm.textBox.value = "";
    return false;
}

</script>
</head>
<body>
<form name="keyForm" id="keyform">
<input type="text" name="textBox" id="textBox" size="20" oncontextmenu="return false" onkeydown="return keyDownListener()" />
</form>
</body>
</html>



#2. Code for handling keyboard event in the case of firefox
<html>
<head>
<script type="text/javascript">

function keyPressListener(myEvent)
{
    var key;
    key = myEvent.keyCode;
    alert('Key Code ' + key);
    document.keyForm.textBox.value = "";
    return false;
}

</script>
</head>
<body>
<form name="keyForm" id="keyform">
<input type="text" name="textBox" id="textBox" size="20" oncontextmenu="return false" onkeypress="return keyPressListener(event)" />
</form>
</body>
</html>



#3. Compatibility issues between firefox and internet explorer for key press event
<html>
<head>
<script type="text/javascript">

function IsIE()
{
    if (navigator.userAgent.indexOf("Firefox")!=-1)
        return false;
    if (navigator.userAgent.indexOf("MSIE")!=-1)
        return true;
}

function keyDownListener(e)
{
    var key, browser;
    alert('Key Down Listener');

    if ( IsIE() ){
        key = window.event.keyCode;
        browser = "Internet Explorer";
    }
    else{
        key = e.keyCode;
        browser = "Firefox";
    }
    alert("Browser: [" + browser + "] Key Code: [" + key + "]");
    document.keyForm.textBox.value = browser;
    return false;
}

function keyPressListener(e)
{
   var key, browser;
   alert('Key Press Listener');

if ( IsIE() ){
        key = window.event.keyCode;
        browser = "Internet Explorer";
    }
    else{
        key = e.keyCode;
        browser = "Firefox";
    }

alert("Browser: [" + browser + "] Key Code: [" + key + "]");
    document.keyForm.textBox.value = browser;
    return false;
}

function detectBrowser()
{
    if(IsIE())
        document.keyForm.textBox.value="Internet Explorer";
    else
        document.keyForm.textBox.value="Firefox";
}

</script>
</head>
<body onload="return detectBrowser()">
<form name="keyForm" id="keyform">
<input type="text" name="textBox" id="textBox" size="20" oncontextmenu="return false" onkeydown="return keyDownListener(event)" onkeypress="return keyPressListener(event)" />
</form>
</body>
</html>


References:
Detecting Keystrokes
Key Events

Read more ...

Mar 28, 2010

A const Reference and a Reference to const object

A reference serves as an alternative name or alias to an object. We cannot define reference to a reference type, but can make a reference to any other data type. C++ imposes several limitations on references. In particular, C++ does not allow you to change which memory a reference points to. Thus, all references are effectively 'const' references. Like normal constant objects, references must be given a value upon declaration.

What should we use then?

MyString& const ref = obj;
OR
MyString const &ref = obj;



Read more ...

Mar 27, 2010

vimrc explained

VIM editor is one of the most popular text editor amongst the developers. At first look, it appears to be a daunting task to get used to it but once you get the hang of the unlimited functionality it offers at your hand, there is no going back and you will fell in love with the tool

A customized ~/.vimrc file

:syntax on        "Turn on syntax highlighting
:set laststatus=2 "Always show status line

:set autowrite    "Automatically write a file when leaving a modified buffer
:set confirm      "Start a dialog when a command fails (here when quit command fails)
:set tabstop=4    "Number of spaces a TAB in the text stands for

:set shiftwidth=4 "Number of spaces used for each step of (auto)indent
:set hlsearch     "Have vim highlight the target of a search
:set incsearch    "Do incremental searching

:set ruler        "Show the cursor position all the time
:set number       "Show line numbers
:set ignorecase   "Ignore case when searching

:set title        "Show info in the window title
:set titlestring=PANKAJ:\ %F   
    "Automatically set screen title

:set noautoindent
:set nosmartindent
:set nocindent


"Indent only if the file is of type cpp,c,java,sh,pl,php,asp
:au FileType cpp,c,java,sh,pl,php,asp  set autoindent
:au FileType cpp,c,java,sh,pl,php,asp  set smartindent
:au FileType cpp,c,java,sh,pl,php,asp  set cindent

"Wrapping long lines
:set wrapmargin=4   "Margin from the right in which to break a line. Set this value to 4 or 5

:set textwidth=70   "Line length above which to break a line

"Defining abbreviations
:ab #d #define
:ab #i #include


"Defining abbreviations to draw comments
:ab #b /********************************************************
:ab #e ********************************************************/
:ab #l /*------------------------------------------------------*/

"Converting tabs to spaces
:set expandtab

VIM Editor Tips

Navigating through VIM

Down (j), Up (k), left (h), right (l)
w (word forward), b (word backward)
W (word forward skip punctuation), B (word backward skip punctuation)
Page Down (Ctrl-f)
Page Up (Ctrl-b)
End of Line ($)
Start of Line (0)
Start of document (gg)
End of Document (G)
Jump to last place that you made an edit (g;)


Copy Pasting Contents

Surprisingly VIM inserts a lot of extra spaces when code is pasted from other applications. This can be corrected by executing the following in VIM mode

:set paste
OR
gg=G

Indentations can be corrected by running the following command in the system

indent -kr filename


Working with Visual Blocks

It is a feature that allows you to mark a block of text and perform editing operations on them. To enter visual blocks mode, press Ctrl+v, then use hjkl or arrow keys to highlight a block of text. Now operate on the first highlighted line as if you were in normal mode

Visual mode (v)
Visual line mode (V)
Visual block mode (Ctrl-v)


Converting tabs to spaces

Make sure you have :set expandtab set in your .vimrc file or otherwise execute set expandtab in normal mode and then run the following command to convert all tabs to spaces

:retab


Wrap a long line

Use (gq) to wrap the highlighted peice of text


Auto-Complete

Start typing the variable/function names and then (Ctrl-n) or (Ctrl-p) next and previous


Delete

delete character (x)
delete word (dw)
delete line (dd)


Auto-Indent

line (==)
entire document (gg=G)


Find/Replace

%s/oldword/newword/g OR %s#oldword#newword#g
%s/oldword/newword/gc OR %s#oldword#newword#gc


Split Windows

Horizontal Windows :split
Vertical Windows :vs
Shifting between Windows (Ctrl-shift-ww)


Map Commands

If you find yourself re-typing the same command over and over, map it to one of the function keys as follows:

:map <fx> cmd

This maps the command cmd to function key Fx. For example, to map F5 to the command :!gcc -c % (compiles the current file) in normal mode


Code Folding

For large code blocks, you might want to fold/hide away certain functions temporarily. To fold a selection, select it in visual then (zf). To open a fold (zo)


Changing the Line Number Background and Foreground color

:highlight LineNr guibg=grey
:highlight LineNr guifg=blue
:highlight LineNr ctermfg=white ctermbg=grey


Read more ...

Apr 14, 2009

Connect to GNOME Display Manager (GDM) using XMing X Server for Windows



The GDM display manager implements all significant features required for managing attached and remote displays.

The GDM daemon can be configured to listen for and manage X Display Manage Protocol (XDMCP) requests from remote displays.

GDM has a number of configuration interfaces. These include scripting integration points, daemon configuration, greeter configuration, general session settings, integration with gnome-settings-daemon configuration, and session configuration.
By default XDMCP support is turned off, but can be enabled if desired.



Daemon Configuration for XDMCP

The GDM daemon is configured using the /etc/gdm/custom.conf file. Default values are stored in GConf in the gdm.schemas file. It is recommended that end-users modify the /etc/gdm/custom.conf file because the schemas file may be overwritten when the user updates their system to have a newer version of GDM.

Note that older versions of GDM supported additional configuration options which are no longer supported in the latest versions of GDM.

The /etc/gdm/custom.conf supports the "[daemon]", "[security]", and "[xdmcp]" group sections


To enable XDMCP Support add the following in custom.conf file

[xdmcp]
Enable=true

[security]
DisallowTCP=false
AllowRemoteRoot=true
Restart X Windows using the command gdm-restart

Connecting to GDM through XMing X Server

Step1: Download XMing X Server executable for Windows from here and install it.

Step2: Launch XLaunch. Select Fullscreen


Step3: Select Open Session via XDMCP


Step4: Enter hostname or ip address


Step5: Click Next


Step6: Click Finish


Step7: You have successfully connected through XMing



References:
1. GNOME Documentation
2. Comparing XDM, GDM, KDM and WDM

Read more ...