From d6949858764602ead48c553327153a215393f77a Mon Sep 17 00:00:00 2001 From: Dan Gudmundsson Date: Fri, 17 Feb 2012 08:32:35 +0100 Subject: [observer] Use wxGC for drawing --- lib/observer/src/observer_app_wx.erl | 85 +++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 40 deletions(-) (limited to 'lib/observer/src/observer_app_wx.erl') diff --git a/lib/observer/src/observer_app_wx.erl b/lib/observer/src/observer_app_wx.erl index 7b4f7c4ce5..cb5f8c1259 100644 --- a/lib/observer/src/observer_app_wx.erl +++ b/lib/observer/src/observer_app_wx.erl @@ -63,6 +63,8 @@ -define(ID_TRACE_TREE_PIDS, 106). -define(ID_TRACE_TREE_NAMES, 107). +-define(wxGC, wxGraphicsContext). + start_link(Notebook, Parent) -> wx_object:start_link(?MODULE, [Notebook, Parent], []). @@ -100,22 +102,22 @@ init([Notebook, Parent]) -> wxPanel:connect(DrawingArea, left_dclick), wxPanel:connect(DrawingArea, right_down), - DefFont = wxSystemSettings:getFont(?wxSYS_DEFAULT_GUI_FONT), + %% DefFont = wxSystemSettings:getFont(?wxSYS_DEFAULT_GUI_FONT), + DefFont = wxFont:new(12,?wxFONTFAMILY_DECORATIVE,?wxFONTSTYLE_NORMAL,?wxFONTWEIGHT_NORMAL), SelCol = wxSystemSettings:getColour(?wxSYS_COLOUR_HIGHLIGHT), + GreyBrush = wxBrush:new({230,230,240}), SelBrush = wxBrush:new(SelCol), LinkPen = wxPen:new(SelCol, [{width, 2}]), - %% GC = wxGraphicsContext:create(DrawingArea), - %% _Font = wxGraphicsContext:createFont(GC, DefFont), process_flag(trap_exit, true), {Panel, #state{parent=Parent, panel =Panel, apps_w=Apps, app_w =DrawingArea, - paint=#paint{font= DefFont, - pen= ?wxBLACK_PEN, - brush=?wxLIGHT_GREY_BRUSH, - sel= SelBrush, - links=LinkPen + paint=#paint{font = DefFont, + pen = wxPen:new({80,80,80}, [{width, 2}]), + brush= GreyBrush, + sel = SelBrush, + links= LinkPen } }}. @@ -219,9 +221,13 @@ handle_sync_event(#wx{event = #wxPaint{}},_, #state{app_w=DA, app=App, sel=Sel, paint=Paint}) -> %% PaintDC must be created in a callback to work on windows. DC = wxPaintDC:new(DA), - wxScrolledWindow:doPrepareDC(DA,DC), + GC = ?wxGC:create(DC), + %% Argh must handle scrolling when using ?wxGC + {Sx,Sy} = wxScrolledWindow:calcScrolledPosition(DA, {0,0}), + ?wxGC:translate(GC, Sx,Sy), %% Nothing is drawn until wxPaintDC is destroyed. - draw(DC, App, Sel, Paint), + draw(GC, App, Sel, Paint), + ?wxGC:destroy(GC), wxPaintDC:destroy(DC), ok. %%%%%%%%%% @@ -250,12 +256,18 @@ handle_info(not_active, State = #state{appmon=AppMon, current=Prev}) -> {noreply, State}; handle_info({delivery, Pid, app_ctrl, _, Apps0}, - State = #state{appmon=Pid, apps_w=LBox}) -> + State = #state{appmon=Pid, apps_w=LBox, current=Curr0}) -> Apps = [atom_to_list(App) || {_, App, {_, _, _}} <- Apps0], wxListBox:clear(LBox), wxListBox:appendStrings(LBox, [App || App <- lists:sort(Apps)]), - {noreply, State}; - + case Apps of + [App|_] when Curr0 =:= undefined -> + Curr = list_to_atom(App), + appmon_info:app(Pid, Curr, true, []), + {noreply, State#state{current=Curr}}; + _ -> + {noreply, State} + end; handle_info({delivery, _Pid, app, _Curr, {[], [], [], []}}, State = #state{panel=Panel}) -> wxWindow:refresh(Panel), @@ -264,7 +276,10 @@ handle_info({delivery, _Pid, app, _Curr, {[], [], [], []}}, handle_info({delivery, Pid, app, Curr, AppData}, State = #state{panel=Panel, appmon=Pid, current=Curr, app_w=AppWin, paint=#paint{font=Font}}) -> - App = build_tree(AppData, {AppWin,Font}), + GC = ?wxGC:create(AppWin), + ?wxGC:setFont(GC, Font, {0,0,0}), + App = build_tree(AppData, {GC,Font}), + ?wxGC:destroy(GC), setup_scrollbar(AppWin, App), wxWindow:refresh(Panel), wxWindow:layout(Panel), @@ -432,15 +447,15 @@ middle([{#box{y=Y0},_}|List], _) -> {#box{y=Y1},_} = lists:last(List), (Y0+Y1) div 2. -box(Str0, N2P, {Win,Font}) -> +box(Str0, N2P, {Win,_Font}) -> Pid = gb_trees:get(Str0, N2P), Str = if hd(Str0) =:= $< -> lists:append(io_lib:format("~w", [Pid])); true -> Str0 end, - {TW,TH, _, _} = wxWindow:getTextExtent(Win, Str, [{theFont, Font}]), + {TW,TH, _, _} = ?wxGC:getTextExtent(Win, Str), Data = #str{text=Str, x=?BX_HE, y=?BY_HE, pid=Pid}, %% Add pid - #box{w=TW+?BX_E, h=TH+?BY_E, s1=Data}. + #box{w=round(TW)+?BX_E, h=round(TH)+?BY_E, s1=Data}. box_to_pid(#box{s1=#str{pid=Pid}}) -> Pid. box_to_reg(#box{s1=#str{text=[$<|_], pid=Pid}}) -> Pid; @@ -458,39 +473,30 @@ draw(_DC, undefined, _, _) -> ok; draw(DC, #app{dim={_W,_H}, ptree=Tree, links=Links}, Sel, #paint{font=Font, pen=Pen, brush=Brush, links=LPen, sel=SelBrush}) -> - %% Canvas = wxGraphicsContext:create(DC), - %% Pen = wxGraphicsContext:createPen(Canvas, ?wxBLACK_PEN), - %% wxGraphicsContext:setPen(Canvas, Pen), - %% Brush = wxGraphicsContext:createBrush(Canvas, ?wxLIGHT_GREY_BRUSH), - %% wxGraphicsContext:setBrush(Canvas, Brush), - %% Font = wxGraphicsContext:createFont(Canvas, wxSystemSettings:getFont(?wxSYS_DEFAULT_GUI_FONT)), - %% wxGraphicsContext:setFont(Canvas, Font), - %% draw_tree(Tree, Canvas). - wxDC:setPen(DC, LPen), + ?wxGC:setPen(DC, LPen), [draw_xlink(Link, DC) || Link <- Links], - wxDC:setPen(DC, Pen), - %% wxDC:drawRectangle(DC, {2,2}, {W-2,H-2}), %% DEBUG - wxDC:setBrush(DC, Brush), - wxDC:setFont(DC, Font), + ?wxGC:setPen(DC, Pen), + %% ?wxGC:drawRectangle(DC, 2,2, _W-2,_H-2), %% DEBUG + ?wxGC:setBrush(DC, Brush), + ?wxGC:setFont(DC, Font, {0,0,0}), draw_tree(Tree, root, DC), case Sel of undefined -> ok; {#box{x=X,y=Y,w=W,h=H,s1=Str1}, _} -> - wxDC:setBrush(DC, SelBrush), - wxDC:drawRoundedRectangle(DC, {X-1,Y-1}, {W+2,H+2}, 8.0), + ?wxGC:setBrush(DC, SelBrush), + ?wxGC:drawRoundedRectangle(DC, X-1,Y-1, W+2,H+2, 8.0), draw_str(DC, Str1, X, Y) end. draw_tree({Box=#box{x=X,y=Y,w=W,h=H,s1=Str1}, Chs}, Parent, DC) -> - %%wxGraphicsContext:drawRoundedRectangle(DC, float(X), float(Y), float(W), float(H), 8.0), - wxDC:drawRoundedRectangle(DC, {X,Y}, {W,H}, 8.0), + ?wxGC:drawRoundedRectangle(DC, X,Y, W,H, 8.0), draw_str(DC, Str1, X, Y), Dot = case Chs of [] -> ok; [{#box{x=CX0},_}|_] -> CY = Y+(H div 2), CX = CX0-(?BB_X div 2), - wxDC:drawLine(DC, {X+W, CY}, {CX, CY}), + ?wxGC:strokeLine(DC, X+W, CY, CX, CY), {CX, CY} end, draw_link(Parent, Box, DC), @@ -500,9 +506,9 @@ draw_link({CX,CY}, #box{x=X,y=Y0,h=H}, DC) -> Y = Y0+(H div 2), case Y =:= CY of true -> - wxDC:drawLine(DC, {CX, CY}, {X, CY}); + ?wxGC:strokeLine(DC, CX, CY, X, CY); false -> - wxDC:drawLines(DC, [{CX, CY}, {CX, Y}, {X,Y}]) + ?wxGC:strokeLines(DC, [{CX, CY}, {CX, Y}, {X,Y}]) end; draw_link(_, _, _) -> ok. @@ -521,9 +527,8 @@ draw_xlink(X0, Y00, X1, Y11, BH, DC) -> {Y0,Y1} = if Y00 < Y11 -> {Y00+BH-6, Y11+6}; true -> {Y00+6, Y11+BH-6} end, - wxDC:drawLines(DC, [{X0,Y0}, {X0+5,Y0}, {X1-5,Y1}, {X1,Y1}]). + ?wxGC:strokeLines(DC, [{X0,Y0}, {X0+5,Y0}, {X1-5,Y1}, {X1,Y1}]). draw_str(DC, #str{x=Sx,y=Sy, text=Text}, X, Y) -> - %%wxGraphicsContext:drawText(DC, Text, float(Sx+X), float(Sy+Y)); - wxDC:drawText(DC, Text, {X+Sx,Y+Sy}); + ?wxGC:drawText(DC, Text, X+Sx,Y+Sy); draw_str(_, _, _, _) -> ok. -- cgit v1.2.3