-
Notifications
You must be signed in to change notification settings - Fork 0
/
pictureview.c
405 lines (330 loc) · 10.5 KB
/
pictureview.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright (c) 2012-2022 Filip "widelec-BB" Maryjanski, BlaBla group.
* All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/cybergraphics.h>
#include <proto/asl.h>
#include <proto/wb.h>
#include <libvstring.h>
#include "globaldefines.h"
#include "locale.h"
#include "prefswindow.h"
#include "kwakwa_api/pictures.h"
#include "support.h"
#include "pictureview.h"
#define PVV_CONTEXTMENU_SAVE_AS 1
#define PVV_CONTEXTMENU_OPEN 2
struct MUI_CustomClass *PictureViewClass;
static IPTR PictureViewDispatcher(VOID);
const struct EmulLibEntry PictureViewGate = {TRAP_LIB, 0, (VOID(*)(VOID))PictureViewDispatcher};
struct PictureViewData
{
APTR pvd_PictureData;
ULONG pvd_PictureDataSize;
struct Picture *pvd_Picture;
Object *pvd_MenuStrip;
struct MUI_EventHandlerNode handler;
ULONG pvd_PrevClickSec, pvd_PrevClickMic;
STRPTR pvd_ShortHelp;
ULONG pvd_GlobalAlpha;
};
struct MUI_CustomClass *CreatePictureViewClass(VOID)
{
struct MUI_CustomClass *cl;
cl = MUI_CreateCustomClass(NULL, MUIC_Area, NULL, sizeof(struct PictureViewData), (APTR)&PictureViewGate);
PictureViewClass = cl;
return cl;
}
VOID DeletePictureViewClass(VOID)
{
if (PictureViewClass) MUI_DeleteCustomClass(PictureViewClass);
}
static IPTR PictureViewNew(Class *cl, Object *obj, struct opSet *msg)
{
obj = (Object*)DoSuperNew(cl, obj,
MUIA_Unicode, TRUE,
MUIA_DoubleBuffer, FALSE, /* virtual text object has own double buffer, so we can't use it here (MUI bug?) */
TAG_MORE, (IPTR)msg->ops_AttrList);
if(obj)
{
struct PictureViewData *d = INST_DATA(cl, obj);
struct TagItem *act_tag, *itr_tag = msg->ops_AttrList;
APTR data = NULL;
STRPTR path = NULL;
ULONG alpha = 0xFFFFFFFF;
d->pvd_MenuStrip = MUI_NewObject(MUIC_Menustrip,
MUIA_Unicode, TRUE,
MUIA_Group_Child, MUI_NewObject(MUIC_Menu,
MUIA_Group_Child, MUI_NewObject(MUIC_Menuitem,
MUIA_Menuitem_Title, GetString(MSG_PICTUREVIEW_MENU_SAVEAS),
MUIA_UserData, PVV_CONTEXTMENU_SAVE_AS,
TAG_END),
MUIA_Group_Child, MUI_NewObject(MUIC_Menuitem,
MUIA_Menuitem_Title, GetString(MSG_PICTUREVIEW_MENU_OPEN),
MUIA_UserData, PVV_CONTEXTMENU_OPEN,
TAG_END),
TAG_END),
TAG_END);
while((act_tag = NextTagItem(&itr_tag)))
{
switch(act_tag->ti_Tag)
{
case PVA_PictureFilePath:
path = (STRPTR)act_tag->ti_Data;
break;
case PVA_PictureData:
data = (APTR)act_tag->ti_Data; /* temporary store pointer to be sure we will know size before allocating memory */
break;
case PVA_PictureDataSize:
d->pvd_PictureDataSize = act_tag->ti_Data;
break;
case MUIA_ShortHelp:
d->pvd_ShortHelp = StrNew((STRPTR)act_tag->ti_Data);
break;
case PVA_GlobalAlpha:
alpha = act_tag->ti_Data;
break;
}
}
d->pvd_GlobalAlpha = alpha;
if(data && d->pvd_PictureDataSize)
{
if((d->pvd_PictureData = AllocMem(d->pvd_PictureDataSize, MEMF_ANY)))
{
QUAD len = d->pvd_PictureDataSize;
CopyMem(data, d->pvd_PictureData, d->pvd_PictureDataSize);
if((d->pvd_Picture = LoadPictureMemory(d->pvd_PictureData, &len)))
{
set(obj, MUIA_ContextMenu, d->pvd_MenuStrip);
return (IPTR)obj;
}
}
}
if(path)
{
if((d->pvd_Picture = LoadPictureFile(path)))
return (IPTR)obj;
}
}
CoerceMethod(cl, obj, OM_DISPOSE);
return (IPTR)NULL;
}
static IPTR PictureViewGet(Class *cl, Object *obj, struct opGet *msg)
{
struct PictureViewData *d = INST_DATA(cl, obj);
int rv = FALSE;
switch(msg->opg_AttrID)
{
case MUIA_ShortHelp:
case MUIA_Text_Contents:
*msg->opg_Storage = (IPTR)(d->pvd_ShortHelp ? d->pvd_ShortHelp : (STRPTR)"");
return TRUE;
case PVA_GlobalAlpha:
*msg->opg_Storage = d->pvd_GlobalAlpha;
return TRUE;
default:
rv = (DoSuperMethodA(cl, obj, (Msg)msg));
}
return rv;
}
static IPTR PictureViewSet(Class *cl, Object *obj, struct opSet *msg)
{
struct PictureViewData *d = INST_DATA(cl, obj);
int tagcount = 0;
struct TagItem *tag = 0, *tagptr = msg->ops_AttrList;
while((tag = NextTagItem(&tagptr)))
{
switch(tag->ti_Tag)
{
case PVA_GlobalAlpha:
d->pvd_GlobalAlpha = tag->ti_Data;
break;
}
}
tagcount += DoSuperMethodA(cl, obj, (Msg)msg);
return tagcount;
}
static IPTR PictureViewDispose(Class *cl, Object *obj, Msg msg)
{
struct PictureViewData *d = INST_DATA(cl, obj);
if(d->pvd_PictureData)
FreeMem(d->pvd_PictureData, d->pvd_PictureDataSize);
if(d->pvd_Picture)
FreePicture(d->pvd_Picture);
if(d->pvd_MenuStrip)
MUI_DisposeObject(d->pvd_MenuStrip);
return DoSuperMethodA(cl, obj, msg);
}
static IPTR PictureViewHandleEvent(Class *cl, Object *obj, struct MUIP_HandleEvent *msg)
{
struct PictureViewData *d = INST_DATA(cl, obj);
struct IntuiMessage *imsg = msg->imsg;
IPTR result = 0;
if(imsg)
{
if(imsg->Class == IDCMP_MOUSEBUTTONS)
{
if(_isinobject(imsg->MouseX, imsg->MouseY))
{
if(imsg->Code == IECODE_LBUTTON)
{
ULONG cur_click_sec, cur_click_mic;
CurrentTime(&cur_click_sec, &cur_click_mic);
if(DoubleClick(d->pvd_PrevClickSec, d->pvd_PrevClickMic, cur_click_sec, cur_click_mic))
{
DoMethod(obj, PVM_OpenPicture);
result |= MUI_EventHandlerRC_Eat;
}
else
{
d->pvd_PrevClickSec = cur_click_sec;
d->pvd_PrevClickMic = cur_click_mic;
}
}
}
}
}
return result;
}
static IPTR PictureViewSetup(Class *cl, Object *obj, struct MUIP_Setup *msg)
{
struct PictureViewData *d = INST_DATA(cl, obj);
IPTR res = DoSuperMethodA(cl, obj, msg);
d->handler.ehn_Class = cl;
d->handler.ehn_Object = obj;
d->handler.ehn_Events = IDCMP_MOUSEBUTTONS;
DoMethod(_win(obj), MUIM_Window_AddEventHandler, &d->handler);
return res;
}
static IPTR PictureViewCleanup(Class *cl, Object *obj, struct MUIP_Cleanup *msg)
{
struct PictureViewData *d = INST_DATA(cl, obj);
DoMethod(_win(obj), MUIM_Window_RemEventHandler, &d->handler);
return DoSuperMethodA(cl, obj, msg);
}
static IPTR PictureViewAskMinMax(Class *cl, Object *obj, struct MUIP_AskMinMax *msg)
{
struct PictureViewData *d = INST_DATA(cl, obj);
IPTR result = DoSuperMethodA(cl, obj, msg);
ULONG max_width = xget(prefs_object(USD_PREFS_TW_PICTURES_MAXWIDTH), MUIA_Slider_Level);
ULONG w, h;
if(max_width == xget(prefs_object(USD_PREFS_TW_PICTURES_MAXWIDTH), MUIA_Slider_Max) || d->pvd_Picture->p_Width < max_width)
{
w = d->pvd_Picture->p_Width;
h = d->pvd_Picture->p_Height;
}
else
{
w = max_width;
h = d->pvd_Picture->p_Height * ((DOUBLE)w) / ((DOUBLE)d->pvd_Picture->p_Width);
}
msg->MinMaxInfo->MinWidth += w;
msg->MinMaxInfo->MinHeight += h;
msg->MinMaxInfo->DefWidth += w;
msg->MinMaxInfo->DefHeight += h;
msg->MinMaxInfo->MaxWidth += w;
msg->MinMaxInfo->MaxHeight += h;
return result;
}
static IPTR PictureViewDraw(Class *cl, Object *obj, struct MUIP_Draw *msg)
{
struct PictureViewData *d = INST_DATA(cl, obj);
IPTR result = DoSuperMethodA(cl, obj, msg);
if(msg->flags & MADF_DRAWOBJECT)
{
if(_mwidth(obj) < d->pvd_Picture->p_Width)
{
ScalePixelArrayAlpha(d->pvd_Picture->p_Data, d->pvd_Picture->p_Width, d->pvd_Picture->p_Height, d->pvd_Picture->p_Width << 2,
_rp(obj), _left(obj), _top(obj), _width(obj), _height(obj), d->pvd_GlobalAlpha);
}
else
{
WritePixelArrayAlpha(d->pvd_Picture->p_Data, 0, 0, d->pvd_Picture->p_Width << 2, _rp(obj), _left(obj),
_top(obj), _width(obj), _height(obj), d->pvd_GlobalAlpha);
}
}
return result;
}
static IPTR PictureViewContextMenuChoice(Class *cl, Object *obj, struct MUIP_ContextMenuChoice *msg)
{
switch(muiUserData(msg->item))
{
case PVV_CONTEXTMENU_SAVE_AS:
return DoMethod(obj, PVM_SavePicture);
case PVV_CONTEXTMENU_OPEN:
return DoMethod(obj, PVM_OpenPicture);
}
return (IPTR)0;
}
static IPTR PictureViewSavePicture(Class *cl, Object *obj)
{
struct PictureViewData *d = INST_DATA(cl, obj);
BOOL result = FALSE;
struct FileRequester *freq;
if((freq = MUI_AllocAslRequestTags(ASL_FileRequest, TAG_END)))
{
STRPTR last_path = LoadFile(GUI_CACHE_DIR "picture_save_asl.cfg", NULL);
if(MUI_AslRequestTags(freq,
ASLFR_Window, _window(obj),
ASLFR_TitleText, GetString(MSG_PICTUREVIEW_ASL_TITLE),
ASLFR_PositiveText, GetString(MSG_PICTUREVIEW_ASL_SAVEAS_POSITIVE),
ASLFR_InitialPattern, "#?",
ASLFR_DoPatterns, TRUE,
ASLFR_RejectIcons, TRUE,
ASLFR_PrivateIDCMP, TRUE,
ASLFR_DoSaveMode, TRUE,
ASLFR_InitialDrawer, last_path ? last_path : (STRPTR)"",
TAG_END))
{
UBYTE location[500];
FmtNPut(location, GUI_CACHE_DIR "picture_save_asl.cfg", sizeof(location));
SaveFile(location, freq->fr_Drawer, StrLen(freq->fr_Drawer));
StrNCopy(freq->fr_Drawer, (STRPTR)location, sizeof(location));
AddPart((STRPTR)location, freq->fr_File, sizeof(location));
if(SaveFile(location, d->pvd_PictureData, d->pvd_PictureDataSize))
result = TRUE;
if(!result)
MUI_Request_Unicode(_app(obj), obj, APP_NAME, GetString(MSG_SEND_PICTURE_FAIL_BUTTONS), GetString(MSG_SEND_PICTURE_FAIL_MSG));
}
if(last_path)
FreeVec(last_path);
MUI_FreeAslRequest(freq);
}
return(IPTR)result;
}
static IPTR PictureViewOpenPicture(Class *cl, Object *obj)
{
struct PictureViewData *d = INST_DATA(cl, obj);
BOOL result = FALSE;
ULONG id = GetUniqueID();
UBYTE buffer[32];
FmtNPut(buffer, "T:.kwakwa_%x", sizeof(buffer), id);
if(SaveFile(buffer, d->pvd_PictureData, d->pvd_PictureDataSize))
result = OpenWorkbenchObjectA(buffer, NULL);
return result;
}
static IPTR PictureViewDispatcher(VOID)
{
Class *cl = (Class*)REG_A0;
Object *obj = (Object*)REG_A2;
Msg msg = (Msg)REG_A1;
switch (msg->MethodID)
{
case OM_NEW: return(PictureViewNew(cl, obj, (struct opSet*)msg));
case OM_GET: return(PictureViewGet(cl, obj, (struct opGet*)msg));
case OM_SET: return(PictureViewSet(cl, obj, (struct opSet*)msg));
case OM_DISPOSE: return(PictureViewDispose(cl, obj, msg));
case MUIM_HandleEvent: return(PictureViewHandleEvent(cl, obj, (struct MUIP_HandleEvent*)msg));
case MUIM_Setup: return(PictureViewSetup(cl, obj, (struct MUIP_Setup*)msg));
case MUIM_Cleanup: return(PictureViewCleanup(cl, obj, (struct MUIP_Cleanup*)msg));
case MUIM_AskMinMax: return(PictureViewAskMinMax(cl, obj, (struct MUIP_AskMinMax*)msg));
case MUIM_Draw: return(PictureViewDraw(cl, obj, (struct MUIP_Draw*)msg));
case MUIM_ContextMenuChoice: return(PictureViewContextMenuChoice(cl, obj, (struct MUIP_ContextMenuChoice*)msg));
case PVM_SavePicture: return(PictureViewSavePicture(cl, obj));
case PVM_OpenPicture: return(PictureViewOpenPicture(cl, obj));
default: return(DoSuperMethodA(cl, obj, msg));
}
}