[olsr-dev] Tray icon for win32

Andreas Tønnesen (spam-protected)
Wed Dec 22 21:25:42 CET 2004


Hi Felix,

This patch sounds very cool :-)
I'll leave it up to Thomas to patch the CVS code when he gets back from
from his vacation as the win32 code is his domain.

- Andreas

Felix Hupfeld wrote:
> Hello everybody,
> 
> I implemented tray icon functionality for the win32 GUI.
> 
> Features
> * Color of tray icon indicates whether olsrd is running and found peers, 
> is running without peers, and is stopped.
> * Tray icon menu has entries for start, stop olsrd, open the 
> configuration window, exit
> * Starting and stopping from the tray icon is synchronized with the 
> corresp. buttons in the main window
> 
> What is mainly missing is closing the main window when pressing "X" or 
> "_" and at startup. Looking at the source, this might need more coding 
> than I wanted to do this evening.
> 
> I hope the attachements include everything needed to patch the CVS.
> 
> Btw... thanks for this fantastic piece of software and the win32 port :)
> 
> ... Felix
> 
> 
> ------------------------------------------------------------------------
> 
> 
> ------------------------------------------------------------------------
> 
> 
> ------------------------------------------------------------------------
> 
> RCS file: /cvsroot/olsrd/olsrd-current/gui/win32/Main/Frontend.cpp,v
> retrieving revision 1.6
> diff -u -r1.6 Frontend.cpp
> --- gui/win32/Main/Frontend.cpp	21 Nov 2004 17:10:27 -0000	1.6
> +++ gui/win32/Main/Frontend.cpp	22 Dec 2004 19:55:09 -0000
> @@ -42,6 +42,7 @@
>  #include "stdafx.h"
>  #include "Frontend.h"
>  #include "FrontendDlg.h"
> +#include "TrayIcon.h"
>  
>  #ifdef _DEBUG
>  #define new DEBUG_NEW
> @@ -106,6 +107,8 @@
>  
>  	CFrontendDlg dlg;
>  
> +	tray_icon = new TrayIcon( dlg, AfxGetInstanceHandle() );
> +
>  	dlg.ConfigFile = CmdLineInfo.m_strFileName;
>  
>  	m_pMainWnd = &dlg;
> Index: gui/win32/Main/Frontend.h
> ===================================================================
> RCS file: /cvsroot/olsrd/olsrd-current/gui/win32/Main/Frontend.h,v
> retrieving revision 1.4
> diff -u -r1.4 Frontend.h
> --- gui/win32/Main/Frontend.h	21 Nov 2004 17:10:27 -0000	1.4
> +++ gui/win32/Main/Frontend.h	22 Dec 2004 19:55:09 -0000
> @@ -52,6 +52,8 @@
>  
>  #include "resource.h"
>  
> +class TrayIcon;
> +
>  class CFrontendApp : public CWinApp
>  {
>  public:
> @@ -61,6 +63,7 @@
>  	unsigned int RedirectThreadFunc(void);
>  
>  	HANDLE OutRead;
> +	TrayIcon* tray_icon;
>  
>  	//{{AFX_VIRTUAL(CFrontendApp)
>  	public:
> Index: gui/win32/Main/Frontend.rc
> ===================================================================
> RCS file: /cvsroot/olsrd/olsrd-current/gui/win32/Main/Frontend.rc,v
> retrieving revision 1.4
> diff -u -r1.4 Frontend.rc
> --- gui/win32/Main/Frontend.rc	20 Nov 2004 22:52:49 -0000	1.4
> +++ gui/win32/Main/Frontend.rc	22 Dec 2004 19:55:10 -0000
> @@ -1,4 +1,4 @@
> -//Microsoft Developer Studio generated resource script.
> +// Microsoft Visual C++ generated resource script.
>  //
>  #include "resource.h"
>  
> @@ -13,7 +13,7 @@
>  #undef APSTUDIO_READONLY_SYMBOLS
>  
>  /////////////////////////////////////////////////////////////////////////////
> -// English (U.S.) resources
> +// Englisch (USA) resources
>  
>  #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
>  #ifdef _WIN32
> @@ -27,18 +27,18 @@
>  // TEXTINCLUDE
>  //
>  
> -1 TEXTINCLUDE DISCARDABLE 
> +1 TEXTINCLUDE 
>  BEGIN
>      "resource.h\0"
>  END
>  
> -2 TEXTINCLUDE DISCARDABLE 
> +2 TEXTINCLUDE 
>  BEGIN
>      "#include ""afxres.h""\r\n"
>      "\0"
>  END
>  
> -3 TEXTINCLUDE DISCARDABLE 
> +3 TEXTINCLUDE 
>  BEGIN
>      "#define _AFX_NO_SPLITTER_RESOURCES\r\n"
>      "#define _AFX_NO_OLE_RESOURCES\r\n"
> @@ -65,11 +65,11 @@
>  //
>  
>  IDD_FRONTEND_DIALOG DIALOGEX 0, 0, 399, 289
> -STYLE DS_MODALFRAME | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | 
> -    WS_SYSMENU
> +STYLE DS_SETFONT | DS_MODALFRAME | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | 
> +    WS_CAPTION | WS_SYSMENU
>  EXSTYLE WS_EX_APPWINDOW
>  CAPTION "olsr.org Switch 0.4.8"
> -FONT 8, "MS Sans Serif"
> +FONT 8, "MS Sans Serif", 0, 0, 0x1
>  BEGIN
>      CONTROL         "Tab1",IDC_TAB1,"SysTabControl32",0x0,7,7,383,256
>      PUSHBUTTON      "Start",IDC_BUTTON1,228,268,50,14
> @@ -77,8 +77,8 @@
>      PUSHBUTTON      "Exit",IDC_BUTTON3,340,268,50,14
>  END
>  
> -IDD_DIALOG1 DIALOG DISCARDABLE  0, 0, 377, 240
> -STYLE WS_CHILD | WS_VISIBLE
> +IDD_DIALOG1 DIALOG  0, 0, 377, 240
> +STYLE DS_SETFONT | WS_CHILD | WS_VISIBLE
>  FONT 8, "MS Sans Serif"
>  BEGIN
>      EDITTEXT        IDC_EDIT1,7,25,363,208,ES_MULTILINE | ES_READONLY | 
> @@ -89,8 +89,8 @@
>      PUSHBUTTON      "Clear",IDC_BUTTON1,320,7,50,14
>  END
>  
> -IDD_DIALOG2 DIALOG DISCARDABLE  0, 0, 377, 240
> -STYLE WS_CHILD | WS_VISIBLE
> +IDD_DIALOG2 DIALOG  0, 0, 377, 240
> +STYLE DS_SETFONT | WS_CHILD | WS_VISIBLE
>  FONT 8, "MS Sans Serif"
>  BEGIN
>      CONTROL         "List1",IDC_LIST1,"SysListView32",LVS_LIST | WS_BORDER | 
> @@ -153,8 +153,8 @@
>      RTEXT           "Window size:",IDC_STATIC,251,171,43,8
>  END
>  
> -IDD_DIALOG3 DIALOG DISCARDABLE  0, 0, 377, 240
> -STYLE WS_CHILD | WS_VISIBLE
> +IDD_DIALOG3 DIALOG  0, 0, 377, 240
> +STYLE DS_SETFONT | WS_CHILD | WS_VISIBLE
>  FONT 8, "MS Sans Serif"
>  BEGIN
>      CONTROL         "List1",IDC_LIST1,"SysListView32",LVS_REPORT | 
> @@ -169,8 +169,8 @@
>      LTEXT           "Node information",IDC_STATIC,257,7,59,8
>  END
>  
> -IDD_DIALOG4 DIALOG DISCARDABLE  0, 0, 377, 240
> -STYLE WS_CHILD | WS_VISIBLE
> +IDD_DIALOG4 DIALOG  0, 0, 377, 240
> +STYLE DS_SETFONT | WS_CHILD | WS_VISIBLE
>  FONT 8, "MS Sans Serif"
>  BEGIN
>      CONTROL         "List1",IDC_LIST1,"SysListView32",LVS_REPORT | WS_BORDER | 
> @@ -185,7 +185,7 @@
>  //
>  
>  #ifdef APSTUDIO_INVOKED
> -GUIDELINES DESIGNINFO DISCARDABLE 
> +GUIDELINES DESIGNINFO 
>  BEGIN
>      IDD_FRONTEND_DIALOG, DIALOG
>      BEGIN
> @@ -237,15 +237,17 @@
>  
>  // Icon with lowest ID value placed first to ensure application icon
>  // remains consistent on all systems.
> -IDI_ICON1               ICON    DISCARDABLE     "res\\Frontend.ico"
> +IDI_ICON1               ICON                    "res\\Frontend.ico"
> +IDI_ICON3               ICON                    "res\\icon3.ico"
> +IDI_ICON2               ICON                    "res\\icon2.ico"
>  
>  /////////////////////////////////////////////////////////////////////////////
>  //
> -// 24
> +// RT_MANIFEST
>  //
>  
> -IDR_241                 24      DISCARDABLE     "Switch.exe.manifest"
> -#endif    // English (U.S.) resources
> +IDR_241                 RT_MANIFEST             "Switch.exe.manifest"
> +#endif    // Englisch (USA) resources
>  /////////////////////////////////////////////////////////////////////////////
>  
>  
> Index: gui/win32/Main/FrontendDlg.cpp
> ===================================================================
> RCS file: /cvsroot/olsrd/olsrd-current/gui/win32/Main/FrontendDlg.cpp,v
> retrieving revision 1.8
> diff -u -r1.8 FrontendDlg.cpp
> --- gui/win32/Main/FrontendDlg.cpp	21 Nov 2004 01:21:10 -0000	1.8
> +++ gui/win32/Main/FrontendDlg.cpp	22 Dec 2004 19:55:11 -0000
> @@ -42,6 +42,7 @@
>  #include "stdafx.h"
>  #include "Frontend.h"
>  #include "FrontendDlg.h"
> +#include "TrayIcon.h"
>  
>  #include "Ipc.h"
>  
> @@ -190,6 +191,11 @@
>  		NodeList.GetNext(Pos);
>  	}
>  
> +	if( NodeList.GetSize() == 0 )
> +		TrayIcon::getInstance()->setStatus( TrayIcon::ON, "No nodes found" );
> +	else
> +		TrayIcon::getInstance()->setStatus( TrayIcon::CONNECTED, "Nodes available" );
> +
>  	m_TabCtrl.m_Dialog3.UpdateNodeInfo(NodeList);
>  }
>  
> @@ -874,6 +880,8 @@
>  
>  int CFrontendDlg::StopOlsrd()
>  {
> +	TrayIcon::getInstance()->setStatus( TrayIcon::OFF, "Off" );
> +
>  	::SetEvent(Event);
>  
>  	::WaitForSingleObject((HANDLE)LogThread, INFINITE);
> @@ -976,6 +984,8 @@
>  
>  void CFrontendDlg::OnExitButton()
>  {
> +	delete TrayIcon::getInstance();
> +
>  	if (StopOlsrd() < 0)
>  		return;
>  
> Index: gui/win32/Main/FrontendDlg.h
> ===================================================================
> RCS file: /cvsroot/olsrd/olsrd-current/gui/win32/Main/FrontendDlg.h,v
> retrieving revision 1.5
> diff -u -r1.5 FrontendDlg.h
> --- gui/win32/Main/FrontendDlg.h	21 Nov 2004 01:21:10 -0000	1.5
> +++ gui/win32/Main/FrontendDlg.h	22 Dec 2004 19:55:11 -0000
> @@ -68,13 +68,13 @@
>  	unsigned int NetThreadFunc(void);
>  
>  	CString ConfigFile;
> +protected:
>  
>  	//{{AFX_VIRTUAL(CFrontendDlg)
> -	protected:
>  	virtual void DoDataExchange(CDataExchange* pDX);
>  	//}}AFX_VIRTUAL
>  
> -protected:
> +public:
>  	//{{AFX_MSG(CFrontendDlg)
>  	virtual BOOL OnInitDialog();
>  	afx_msg void OnOK();
> @@ -84,8 +84,12 @@
>  	afx_msg void OnExitButton();
>  	//}}AFX_MSG
>  
> +protected:
>  	DECLARE_MESSAGE_MAP()
>  
> +	int StartOlsrd(void);
> +	int StopOlsrd(void);
> +
>  	int GetInterfaces(void);
>  
>  	HANDLE Event;
> @@ -94,9 +98,6 @@
>  
>  	SOCKET SockHand;
>  
> -	int StartOlsrd(void);
> -	int StopOlsrd(void);
> -
>  	int PipeMode;
>  	int ExecutePipe(const char *, HANDLE *, HANDLE *, HANDLE *);
>  
> Index: gui/win32/Main/resource.h
> ===================================================================
> RCS file: /cvsroot/olsrd/olsrd-current/gui/win32/Main/resource.h,v
> retrieving revision 1.3
> diff -u -r1.3 resource.h
> --- gui/win32/Main/resource.h	20 Nov 2004 22:52:49 -0000	1.3
> +++ gui/win32/Main/resource.h	22 Dec 2004 19:55:11 -0000
> @@ -1,5 +1,5 @@
>  //{{NO_DEPENDENCIES}}
> -// Microsoft Developer Studio generated include file.
> +// Microsoft Visual C++ generated include file.
>  // Used by Frontend.rc
>  //
>  #define IDD_FRONTEND_DIALOG             102
> @@ -11,6 +11,8 @@
>  #define IDD_DIALOG4                     132
>  #define IDI_ICON1                       132
>  #define IDR_241                         138
> +#define IDI_ICON3                       141
> +#define IDI_ICON2                       142
>  #define IDC_TAB1                        1000
>  #define IDC_BUTTON1                     1001
>  #define IDC_EDIT1                       1003
> @@ -48,7 +50,7 @@
>  // 
>  #ifdef APSTUDIO_INVOKED
>  #ifndef APSTUDIO_READONLY_SYMBOLS
> -#define _APS_NEXT_RESOURCE_VALUE        140
> +#define _APS_NEXT_RESOURCE_VALUE        141
>  #define _APS_NEXT_COMMAND_VALUE         32771
>  #define _APS_NEXT_CONTROL_VALUE         1030
>  #define _APS_NEXT_SYMED_VALUE           101
> 
> 
> ------------------------------------------------------------------------
> 
> #include "stdafx.h"
> #include "TrayIcon.h"
> 
> #include <windows.h>
> #include <shellapi.h>
> 
> #include "Frontend.h"
> #include "FrontendDlg.h"
> #include "Resource.h"
> 
> #define TRAYICONID 987435
> 
> HWND s_hWnd;
> TrayIcon* TrayIcon::instance = NULL;
> 
> LRESULT CALLBACK TrayIconProc( HWND, UINT, WPARAM, LPARAM lParam )
> {
> 	switch (lParam)
> 	{
> 		case WM_LBUTTONDOWN:
> 		case WM_RBUTTONDOWN:
> 			TrayIcon::instance->displayPopup();
> 			break;
> 	}
> 
> 	return 1;
> }
> 
> void TrayIcon::displayPopup()
> {
> 	HMENU hMenu = CreatePopupMenu();
> 
> 	MENUITEMINFO item;
> 	memset(&item, 0, sizeof(MENUITEMINFO));
> 	item.cbSize = sizeof(MENUITEMINFO);
> 	item.fMask = MIIM_TYPE | MIIM_ID;
> 	item.fType = MFT_STRING;
> 
> 	item.wID = 1;
> 	item.dwTypeData = "&Start";
> 	InsertMenuItem(hMenu, 1, TRUE, &item);
> 
> 	item.wID = 2;
> 	item.dwTypeData = "&Stop";
> 	InsertMenuItem(hMenu, 2, TRUE, &item);
> 
> 	item.wID = 3;
> 	item.dwTypeData = "&Configure";
> 	InsertMenuItem(hMenu, 3, TRUE, &item);
> 
> 	item.wID = 4;
> 	item.dwTypeData = "&Exit";
> 	InsertMenuItem(hMenu, 4, TRUE, &item);
> 
> 	POINT pt;
> 
> 	GetCursorPos(&pt);
> 
> 	SetForegroundWindow( s_hWnd );
> 
> 	int cmd = TrackPopupMenu(hMenu, 
> 			TPM_RIGHTBUTTON | TPM_RETURNCMD, 
> 			pt.x, 
> 			pt.y, 
> 			0, 
> 			s_hWnd, 
> 			NULL);
> 
>    PostMessage(s_hWnd, WM_NULL, 0, 0); 
> 
> 	switch(cmd)
> 	{
> 		case 4:
> 			PostQuitMessage(0);
> 			break;
> 	
> 		case 3:
> 			main_dlg.OpenIcon();
> 			break;
> 
> 		case 2:
> 			main_dlg.OnStopButton();
> 			break;
> 
> 		case 1:
> 			main_dlg.OnStartButton();
> 			break;
> 	}
> 
> 	DestroyMenu(hMenu);
> }
> 
> void TrayIcon::setStatus( status con_stat, const char* message )
> {
> 	switch( con_stat )
> 	{
> 		case CONNECTED:
> 			setTrayAppearance( false, IDI_ICON2, message );
> 			break;
> 
> 		case OFF:
> 			setTrayAppearance( false, IDI_ICON3, message );
> 			break;
> 
> 		case ON:
> 			setTrayAppearance( false, IDI_ICON1, message );
> 			break;
> 	}
> }
> 
> void TrayIcon::setTrayAppearance( bool start, unsigned int res_id, const char* message )
> {
> 	NOTIFYICONDATA notifyIconData;
> 
> 	notifyIconData.cbSize = sizeof(notifyIconData);
> 	notifyIconData.hWnd = s_hWnd;
> 
> 	notifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
> 	notifyIconData.uCallbackMessage = 123456;
> 	notifyIconData.uID = TRAYICONID; 
> 	notifyIconData.hIcon = (HICON)LoadIcon( hInst, MAKEINTRESOURCE( res_id ) );  
> 	strcpy( notifyIconData.szTip, message ); 
> 
> 	if( start )
> 		Shell_NotifyIcon( NIM_ADD, &notifyIconData );
> 	else
> 		Shell_NotifyIcon( NIM_MODIFY, &notifyIconData );
> }
> 
> TrayIcon::TrayIcon( CFrontendDlg& dlg, HINSTANCE hInst ) : main_dlg( dlg ), hInst( hInst )
> {
> 	instance = this;
> 	WNDCLASS wndClass;
> 
> 	wndClass.style = 0; 
> 	wndClass.lpfnWndProc = TrayIconProc;
> 	wndClass.cbClsExtra = 0;
> 	wndClass.cbWndExtra = 0;
> 	wndClass.hInstance = hInst;
> 	wndClass.hIcon = NULL;
> 	wndClass.hCursor = NULL;
> 	wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
> 	wndClass.lpszMenuName = NULL;
> 	wndClass.lpszClassName = "OLSRdTrayIcon";
> 
>    if (0 != RegisterClass(&wndClass))
>    {
>       s_hWnd = CreateWindow( 
> 		 "OLSRdTrayIcon", 
>          "Invisible Message Window", 
>          WS_OVERLAPPEDWINDOW,
>          CW_USEDEFAULT,
>          CW_USEDEFAULT,
>          CW_USEDEFAULT,
>          CW_USEDEFAULT,
>          NULL,
>          NULL,
>          hInst,
>          NULL);
> 
>       if (s_hWnd)
>       {
>          ShowWindow(s_hWnd, SW_HIDE);
>          
>          UpdateWindow(s_hWnd);
>       }
>    }
> 
>    setTrayAppearance( true, IDI_ICON3, "Starting..." );
> }
> 
> TrayIcon::~TrayIcon()
> {
> 	NOTIFYICONDATA notifyIconData;
> 
> 	notifyIconData.cbSize = sizeof(notifyIconData);
> 	notifyIconData.hWnd = s_hWnd;
> 
> 	notifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
> 	notifyIconData.uCallbackMessage = 123456;
> 	notifyIconData.uID = TRAYICONID; 
> 	notifyIconData.hIcon = NULL;
> 
> 	Shell_NotifyIcon( NIM_DELETE, &notifyIconData );
> }
> 
> 
> ------------------------------------------------------------------------
> 
> #ifndef TRAYICON_H_171149531
> #define TRAYICON_H_171149531
> 
> class CFrontendDlg;
> 
> class TrayIcon
> {
> public:
> 	TrayIcon( CFrontendDlg&, HINSTANCE );
> 	~TrayIcon();
> 
> 	void displayPopup();	
> 
> 	static TrayIcon* getInstance()	{ return instance; }
> 
> 	enum status { CONNECTED, ON, OFF };
> 
> 	void setStatus( status con_status, const char* message );
> 
> private:
> 	void setTrayAppearance( bool, unsigned int, const char* message );
> 
> 	HINSTANCE hInst;
> 
> 	CFrontendDlg& main_dlg;
> 
> 	friend LRESULT CALLBACK TrayIconProc( HWND, UINT, WPARAM, LPARAM );
> 	static TrayIcon* instance;
> };
> 
> #endif // TRAYICON_H_171149531
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> olsr-dev mailing list
> (spam-protected)
> https://www.olsr.org/mailman/listinfo/olsr-dev

-- 
Andreas Tønnesen
http://www.olsr.org



More information about the Olsr-dev mailing list