1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2009-2016. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19 20-module(ex_graphicsContext). 21 22-behaviour(wx_object). 23 24%% Client API 25-export([start/1]). 26 27%% wx_object callbacks 28-export([init/1, terminate/2, code_change/3, 29 handle_info/2, handle_call/3,handle_cast/2, 30 handle_event/2, handle_sync_event/3]). 31 32-include_lib("wx/include/wx.hrl"). 33 34-record(state, 35 { 36 parent, 37 config, 38 win, 39 pen, 40 brush, 41 font 42 }). 43 44start(Config) -> 45 wx_object:start_link(?MODULE, Config, []). 46 47%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 48init(Config) -> 49 wx:batch(fun() -> do_init(Config) end). 50 51do_init(Config) -> 52 Parent = proplists:get_value(parent, Config), 53 Panel = wxPanel:new(Parent, []), 54 55 %% Setup sizers 56 MainSizer = wxBoxSizer:new(?wxVERTICAL), 57 Sizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel, 58 [{label, "wxGraphicsContext"}]), 59 60 Win = wxPanel:new(Panel, []), 61 Pen = ?wxBLACK_PEN, 62 Brush = wxBrush:new({30, 175, 23, 127}), 63 Font = ?wxITALIC_FONT, 64 wxPanel:connect(Win, paint, [callback]), 65 66 %% Add to sizers 67 wxSizer:add(Sizer, Win, [{flag, ?wxEXPAND}, 68 {proportion, 1}]), 69 wxSizer:add(MainSizer, Sizer, [{flag, ?wxEXPAND}, 70 {proportion, 1}]), 71 72 wxPanel:setSizer(Panel, MainSizer), 73 {Panel, #state{parent=Panel, config=Config, win = Win, 74 pen = Pen, brush = Brush, font = Font}}. 75 76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 77%% Sync events i.e. from callbacks must return ok, it cannot return a new state. 78%% Do the redrawing here. 79handle_sync_event(#wx{event = #wxPaint{}},_, 80 #state{win=Win, pen = Pen, brush = Brush, font = Font}) -> 81 %% PaintDC must be created in a callback to work on windows. 82 DC = wxPaintDC:new(Win), 83 %% Nothing is drawn until wxPaintDC is destroyed. 84 draw(DC, Pen, Brush, Font), 85 wxPaintDC:destroy(DC), 86 ok. 87%% Async Events are handled in handle_event as in handle_info 88handle_event(#wx{}, State) -> 89 {noreply, State}. 90 91%% Callbacks handled as normal gen_server callbacks 92handle_info(Msg, State) -> 93 demo:format(State#state.config, "Got Info ~p\n", [Msg]), 94 {noreply, State}. 95 96handle_call(shutdown, _From, State=#state{parent=Panel}) -> 97 wxPanel:destroy(Panel), 98 {stop, normal, ok, State}; 99 100handle_call(Msg, _From, State) -> 101 demo:format(State#state.config, "Got Call ~p\n", [Msg]), 102 {reply,{error, nyi}, State}. 103 104handle_cast(Msg, State) -> 105 io:format("Got cast ~p~n",[Msg]), 106 {noreply,State}. 107 108code_change(_, _, State) -> 109 {stop, ignore, State}. 110 111terminate(_Reason, _State) -> 112 ok. 113 114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 115%% Local functions 116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 117 118draw(Win, Pen, Brush, Font) -> 119 try 120 Canvas = wxGraphicsContext:create(Win), 121 wxGraphicsContext:setPen(Canvas, Pen), 122 wxGraphicsContext:setBrush(Canvas, Brush), 123 wxGraphicsContext:setFont(Canvas, Font, {0, 0, 50}), 124 125 wxGraphicsContext:drawRoundedRectangle(Canvas, 35.0,35.0, 100.0, 50.0, 10.0), 126 wxGraphicsContext:drawText(Canvas, "This text should be antialised", 60.0, 55.0), 127 Path = wxGraphicsContext:createPath(Canvas), 128 wxGraphicsPath:addCircle(Path, 0.0, 0.0, 40.0), 129 wxGraphicsPath:closeSubpath(Path), 130 wxGraphicsContext:translate(Canvas, 100.0, 250.0), 131 132 F = fun(N) -> 133 wxGraphicsContext:scale(Canvas, 1.1, 1.1), 134 wxGraphicsContext:translate(Canvas, 15.0,-1.0*N), 135 wxGraphicsContext:drawPath(Canvas, Path) 136 end, 137 wx:foreach(F, lists:seq(1,10)), 138 wxGraphicsObject:destroy(Path), 139 wxGraphicsObject:destroy(Canvas), 140 ok 141 catch _:{not_supported, _} -> 142 Err = "wxGraphicsContext not available in this build of wxwidgets", 143 io:format(Err,[]) 144 end. 145 146