wxTaskBarIcon

BlitzMax Modules Forums/Brucey's Modules/wxTaskBarIcon

Hi!
There is no wxTaskBarIcon.mod ?

I try to wrap it, but my c++ knowlege is not enaugh for it:
- create wxtaskbaricon.mod directory in mod/wx.mod/
- create glue.h and glue.cpp
- create common.bmx and wxtaskbaricon.bmx

glue.h
/*
  Copyright (c) 2007,2008 Bruce A Henderson
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/ 

#include "wxglue.h"
#include "wx/taskbar.h"
#include "../wxmenu.mod/glue.h"
#include "../wxicon.mod/glue.h"

class MaxTaskBarIcon;

extern "C" {

#include <blitz.h>

	MaxTaskBarIcon * bmx_wxtaskbaricon_create(BBObject * maxHandle);
	//wxMenu * bmx_wxtaskbaricon_createpopupmenu(wxTaskBarIcon * taskBarIcon);
	bool bmx_wxtaskbaricon_isiconinstalled(wxTaskBarIcon * taskBarIcon);
	bool bmx_wxtaskbaricon_isok(wxTaskBarIcon * taskBarIcon);
	bool bmx_wxtaskbaricon_popupmenu(wxTaskBarIcon * taskBarIcon, wxMenu * menu);
	bool bmx_wxtaskbaricon_removeicon(wxTaskBarIcon * taskBarIcon);
	bool bmx_wxtaskbaricon_seticon(wxTaskBarIcon * taskBarIcon, MaxIcon * icon, BBString * tooltip);
	void bmx_wxtaskbaricon_free(wxTaskBarIcon * taskBarIcon);

	int bmx_wxtaskbariconevent_geteventtype(int type);
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


class MaxTaskBarIcon : public wxTaskBarIcon
{
public:
	MaxTaskBarIcon(BBObject * handle);
	~MaxTaskBarIcon();
};


glue.cpp
/*
  Copyright (c) 2007,2008 Bruce A Henderson
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/ 

#include "glue.h"

// ---------------------------------------------------------------------------------------


MaxTaskBarIcon::MaxTaskBarIcon(BBObject * handle)
	: wxTaskBarIcon()
{
	wxbind(this, handle);
}

MaxTaskBarIcon::~MaxTaskBarIcon() {
	wxunbind(this);
}

// *********************************************


MaxTaskBarIcon * bmx_wxtaskbaricon_create(BBObject * maxHandle) {
	return new MaxTaskBarIcon(maxHandle);
}

/*
wxMenu * bmx_wxtaskbaricon_createpopupmenu(wxTaskBarIcon * taskBarIcon) {
	return taskBarIcon->CreatePopupMenu();
}
*/

bool bmx_wxtaskbaricon_isiconinstalled(wxTaskBarIcon * taskBarIcon) {
	return taskBarIcon->IsIconInstalled();
}

bool bmx_wxtaskbaricon_isok(wxTaskBarIcon * taskBarIcon) {
	return taskBarIcon->IsOk();
}

bool bmx_wxtaskbaricon_popupmenu(wxTaskBarIcon * taskBarIcon, wxMenu * menu) {
	return taskBarIcon->PopupMenu(menu);
}

bool bmx_wxtaskbaricon_removeicon(wxTaskBarIcon * taskBarIcon) {
	return taskBarIcon->RemoveIcon();
}

bool bmx_wxtaskbaricon_seticon(wxTaskBarIcon * taskBarIcon, MaxIcon * icon, BBString * tooltip) {
	return taskBarIcon->SetIcon(icon->Icon(), wxStringFromBBString(tooltip));
}

void bmx_wxtaskbaricon_free(wxTaskBarIcon * taskBarIcon) {
	delete taskBarIcon;
}

int bmx_wxtaskbariconevent_geteventtype(int type) {
	switch(type) {
		case 1550: return wxEVT_TASKBAR_MOVE;
		case 1551: return wxEVT_TASKBAR_LEFT_DOWN;
		case 1552: return wxEVT_TASKBAR_LEFT_UP;
		case 1553: return wxEVT_TASKBAR_RIGHT_DOWN;
		case 1554: return wxEVT_TASKBAR_RIGHT_UP;
		case 1555: return wxEVT_TASKBAR_LEFT_DCLICK;
		case 1556: return wxEVT_TASKBAR_RIGHT_DCLICK;
	}
	
	return 0;
}


common.bmx
' Copyright (c) 2007,2008 Bruce A Henderson
' 
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
' 
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
' THE SOFTWARE.
' 
SuperStrict

Import wx.wx
Import BRL.Blitz


' headers :-)
?linux
Import "../lib/linux/wx/include/gtk2-unicode-release-static/*.h"
Import "../include/*.h"
?win32
Import "../lib/win32/mswu/*.h"
Import "../include/*.h"
?macosppc
Import "../lib/macosppc/wx/include/mac-unicode-release-static/*.h"
Import "../include/*.h"
?macosx86
Import "../lib/macosx86/wx/include/mac-unicode-release-static/*.h"
Import "../include/*.h"
?

Import "glue.cpp"

Extern
	Function bmx_wxtaskbaricon_create:Byte Ptr(handle:Object)
	Function bmx_wxtaskbaricon_createpopupmenu:Byte Ptr(handle:Byte Ptr)
	Function bmx_wxtaskbaricon_isiconinstalled:Int(handle:Byte Ptr)
	Function bmx_wxtaskbaricon_isok:Int(handle:Byte Ptr)
	Function bmx_wxtaskbaricon_popupmenu:Int(handle:Byte Ptr, menu:Byte Ptr)
	Function bmx_wxtaskbaricon_removeicon:Int(handle:Byte Ptr)
	Function bmx_wxtaskbaricon_seticon:Int(handle:Byte Ptr, icon:Byte Ptr, tooltip:String)
	Function bmx_wxtaskbaricon_free(handle:Byte Ptr)

	Function bmx_wxtaskbariconevent_geteventtype:Int(evt:Int)
End Extern

Const wxEVT_TASKBAR_MOVE:Int = 1550
Const wxEVT_TASKBAR_LEFT_DOWN:Int = 1551
Const wxEVT_TASKBAR_LEFT_UP:Int = 1552
Const wxEVT_TASKBAR_RIGHT_DOWN:Int = 1553
Const wxEVT_TASKBAR_RIGHT_UP:Int = 1554
Const wxEVT_TASKBAR_LEFT_DCLICK:Int = 1555
Const wxEVT_TASKBAR_RIGHT_DCLICK:Int = 1556
?win32
	Const wxEVT_TASKBAR_CLICK:Int = wxEVT_TASKBAR_RIGHT_UP
?Not win32
	Const wxEVT_TASKBAR_CLICK:Int = wxEVT_TASKBAR_RIGHT_DOWN
?


wxtaskbaricon.bmx
' Copyright (c) 2007,2008 Bruce A Henderson
' 
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
' 
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
' THE SOFTWARE.
' 
SuperStrict

Rem
bbdoc: wxMenu
End Rem
Module wx.wxTaskBarIcon

ModuleInfo "Version: 1.00"
ModuleInfo "License: MIT"
ModuleInfo "Author: Bruce A Henderson"
ModuleInfo "Copyright: (c) 2007 Bruce A Henderson"
ModuleInfo "Modserver: BRL"

?linux
ModuleInfo "CC_OPTS: -D__WXGTK__"
ModuleInfo "CC_OPTS: -D_FILE_OFFSET_BITS=64"
ModuleInfo "CC_OPTS: -D_LARGE_FILES"
ModuleInfo "CC_OPTS: -DWX_PRECOMP"
?win32
ModuleInfo "CC_OPTS: -DHAVE_W32API_H"
ModuleInfo "CC_OPTS: -D__WXMSW__"
ModuleInfo "CC_OPTS: -D_UNICODE"
ModuleInfo "CC_OPTS: -DUNICODE"
?macos
ModuleInfo "CC_OPTS: -D__WXMAC__"
ModuleInfo "CC_OPTS: -D_FILE_OFFSET_BITS=64"
ModuleInfo "CC_OPTS: -D_LARGE_FILES"
ModuleInfo "CC_OPTS: -DWX_PRECOMP"
?

Import "common.bmx"
Import wx.wxMenu
Import wx.wxIcon

Rem
bbdoc: This class represents a taskbar icon.
about: A taskbar icon is an icon that appears in the 'system tray' and
responds to mouse clicks, optionally with a tooltip above it to help provide information.
<p>
X Window System Note:<br />
Under X Window System, the window manager must support either the System Tray Protocol
by freedesktop.org (WMs used by modern desktop environments such as GNOME >= 2, KDE >= 3
and XFCE >= 4 all do) or the older methods used in GNOME 1.2 and KDE 1 and 2. If it doesn't,
the icon will appear as a toplevel window on user's desktop.

Because not all window managers have system tray, there's no guarantee that wxTaskBarIcon will
work correctly under X Window System and so the applications should use it only as an optional
component of their user interface. The user should be required to explicitly enable the taskbar
icon on Unix, it shouldn't be on by default.
</p>
End Rem
Type wxTaskBarIcon Extends wxEvtHandler
	' creates a "soft link" to a wxMenu*
	Function _create:wxTaskBarIcon(wxObjectPtr:Byte Ptr)
		If wxObjectPtr Then
			Local this:wxTaskBarIcon = New wxTaskBarIcon
			this.wxObjectPtr = wxObjectPtr
			Return this
		End If
	End Function
	
	Function _find:wxTaskBarIcon(wxObjectPtr:Byte Ptr)
		If wxObjectPtr Then
			Local taskBarIcon:wxTaskBarIcon = wxTaskBarIcon(wxfind(wxObjectPtr))
			If Not taskBarIcon Then
				Return _create(wxObjectPtr)
			End If
			Return taskBarIcon
		End If
	End Function

	Rem
	bbdoc: Constructs a #wxTaskBarIcon object.
	about: TODO
	End Rem
	Function CreateTaskBarIcon:wxTaskBarIcon()
		Return New wxTaskBarIcon.Create()
	End Function

	Rem
	bbdoc: 
	End Rem
	Method Create:wxTaskBarIcon()
		wxObjectPtr = bmx_wxtaskbaricon_create(Self)
		OnInit()
		Return Self
	End Method

	Rem
	bbdoc: Deletes this TaskBarIcon
	End Rem
	Method Free()
		If wxObjectPtr Then
			bmx_wxtaskbaricon_free(wxObjectPtr)
			wxObjectPtr = Null
		End If
	End Method
	
	Rem
	bbdoc: This method is called by the library when the user requests popup menu.
	about: This method is called by the library when the user requests popup menu (on Windows and Unix platforms,
	this is when the user right-clicks the icon). Override this function in order to provide popup menu associated
	with the icon.
    If CreatePopupMenu returns NULL (this happens by default), no menu is shown, otherwise the menu is displayed
    and then deleted by the library as soon as the user dismisses it. The events can be handled by a class derived
    from wxTaskBarIcon.
	End Rem
Rem
	Method CreatePopupMenu:wxMenu()
		Return bmx_wxtaskbaricon_createpopupmenu(wxObjectPtr)
	End Method
End Rem

	Rem
	bbdoc: Returns true if SetIcon was called with no subsequent RemoveIcon.
	about: TODO
	End Rem
	Method IsIconInstalled:Int()
		Return bmx_wxtaskbaricon_isiconinstalled(wxObjectPtr)
	End Method
	
	Rem
	bbdoc: Returns true if the object initialized successfully.
	about: TODO
	End Rem
	Method IsOk:Int()
		Return bmx_wxtaskbaricon_isok(wxObjectPtr)
	End Method
	
	Rem
	bbdoc: Pops up a menu at the current mouse position.
	about: The events can be handled by a class derived from #wxTaskBarIcon.
	It is recommended to override #CreatePopupMenu callback instead of calling this method from event handler,
	because some ports (e.g. wxCocoa) may not implement PopupMenu and mouse click events at all.
	End Rem
	Method PopupMenu:Int(menu:wxMenu)
		Return bmx_wxtaskbaricon_popupmenu(wxObjectPtr, menu.wxObjectPtr)
	End Method
	
	Rem
	bbdoc: Removes the icon previously set with SetIcon.
	about: TODO
	End Rem
	Method RemoveIcon:Int()
		Return bmx_wxtaskbaricon_removeicon(wxObjectPtr)
	End Method
	
	Rem
	bbdoc: Sets the icon, and optional tooltip text.
	about: TODO
	End Rem
	Method SetIcon:Int(icon:wxIcon, tooltip:String)
		Return bmx_wxtaskbaricon_seticon(wxObjectPtr, icon.wxObjectPtr, tooltip)
	End Method
End Type


Type wxTaskBarIconEvent Extends wxEvent

	Function Create:wxEvent(wxEventPtr:Byte Ptr, evt:TEventHandler)
		Local this:wxTaskBarIconEvent = New wxTaskBarIconEvent 
		
		this.init(wxEventPtr, evt)
		
		Return this
	End Function
End Type

Type TTaskBarIconEventFactory Extends TEventFactory

	Method CreateEvent:wxEvent(wxEventPtr:Byte Ptr, evt:TEventHandler)
	
		Select evt.eventType
			Case wxEVT_TASKBAR_MOVE, ..
					wxEVT_TASKBAR_LEFT_DOWN, ..
					wxEVT_TASKBAR_LEFT_UP, ..
					wxEVT_TASKBAR_RIGHT_DOWN, ..
					wxEVT_TASKBAR_RIGHT_UP, ..
					wxEVT_TASKBAR_LEFT_DCLICK, ..
					wxEVT_TASKBAR_RIGHT_DCLICK
				Return wxTaskBarIconEvent.Create(wxEventPtr, evt)
		End Select
		
		Return Null
	End Method

	Method GetEventType:Int(eventType:Int)
		Select eventType
			Case wxEVT_TASKBAR_MOVE, ..
					wxEVT_TASKBAR_LEFT_DOWN, ..
					wxEVT_TASKBAR_LEFT_UP, ..
					wxEVT_TASKBAR_RIGHT_DOWN, ..
					wxEVT_TASKBAR_RIGHT_UP, ..
					wxEVT_TASKBAR_LEFT_DCLICK, ..
					wxEVT_TASKBAR_RIGHT_DCLICK
				Return bmx_wxtaskbariconevent_geteventtype(eventType)
		End Select
	End Method
	
End Type

New TTaskBarIconEventFactory


I don't know how to wrap virtual wxMenu* CreatePopupMenu().

Can you help?

cu olli

10 points for effort :-)

^^ it works now(code is edited)

SuperStrict

Framework wx.wxApp
Import wx.wxFrame
Import wx.wxTimer
Import wx.wxTaskBarIcon

Global Application : TApplication

Application = New TApplication
Application.Run()

Type TApplication Extends wxApp
	Field Frame       : TFrame

	Method OnInit:Int()
		Frame = New TFrame
		Frame.Create(,, "wxTaskBarIcon Test")
		Frame.Show()
		SetTopWindow(Frame)
		
		Return True
	End Method
End Type

Type TFrame Extends wxFrame
	Field Icon        : wxIcon
	Field TaskbarIcon : wxTaskBarIcon
	Field Timer       : wxTimer

	Method OnInit()
		Icon = New wxIcon.CreateFromFile("icon.ico", wxBITMAP_TYPE_ICO)
		If Not Icon.IsOk() Then Throw("Unable to load `icon.ico`")
		TaskBarIcon = New wxTaskBarIcon.Create()
		Timer = New wxTimer.Create(Self)
		
		ConnectAny(wxEVT_TIMER, OnTick)
		
		Timer.Start(1000)
	End Method
	
	Function OnTick(E:wxEvent)
		Global Show : Int
		
		Local Event : wxTimerEvent, ..
		      _Self : TFrame
		
		Event = wxTimerEvent(E)
		_Self = TFrame(Event.Parent)
		
		Show = Not Show
		If Show Then
			_Self.TaskBarIcon.SetIcon(_Self.Icon, "Hello, world!")
		Else
			_Self.TaskBarIcon.RemoveIcon()
		EndIf
	End Function
End Type


But I don't know how to solve the problem with CreatePopupMenu and if its the right memory management.

cu olli

Hi :-)

I've checked in your wxTaskBarIcon, with some minor tweaks, and the CreatePopupMenu functionality (although I haven't tested it yet).

Thanks!