de-lint. disable c++ parentheses warnings. fix casts in AROSPDFApp.cc
[AROS-Contrib.git] / vpdf / window.c
bloba7825c39c6fc4d459f5e4a49d171d81a57da7254
2 #if defined(__AROS__)
3 #define MUIMASTER_YES_INLINE_STDARG
4 #endif
6 /// System includes
7 #define AROS_ALMOST_COMPATIBLE
8 #include <proto/muimaster.h>
9 #include <libraries/mui.h>
11 #include <libraries/asl.h>
12 #include <workbench/workbench.h>
14 #include <proto/exec.h>
15 #include <intuition/intuition.h>
16 #include <graphics/gfx.h>
17 #include <exec/libraries.h>
18 #include <proto/graphics.h>
19 #include <proto/intuition.h>
20 #include <proto/icon.h>
21 #include <dos/dos.h>
22 #include <proto/dos.h>
23 #include <proto/locale.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <math.h>
29 #include <memory.h>
31 #include <proto/alib.h>
32 #include <proto/utility.h>
34 #include <proto/datatypes.h>
35 #include <proto/dtclass.h>
36 #include <datatypes/pictureclass.h>
37 #include <devices/rawkeycodes.h>
39 #include <libraries/gadtools.h>
41 #if !defined(__AROS__)
42 #include <emul/emulregs.h>
43 #include <emul/emulinterface.h>
44 #endif
45 ////
47 #include <private/vapor/vapor.h>
48 #include "util.h"
49 #include "poppler.h"
50 #include "locale.h"
51 #include "application.h"
52 #include "window.h"
53 #include "settings.h"
54 #include "system/functions.h"
56 #include "mcc/documentview_class.h"
57 #include "mcc/documentlayout_class.h"
58 #include "mcc/toolbar_class.h"
59 #include "mcc/renderer_class.h"
60 #include "mcc/title_class.h"
62 #define MAXRECENT 10
64 struct Data
66 unsigned int id;
67 char muiid[64];
68 Object *grpRoot;
69 Object *grpTitles;
70 Object *btnErrors;
71 char title[256];
74 DEFNEW
76 Object *grpRoot, *grpTitles;
78 obj = DoSuperNew(cl, obj,
79 MUIA_Window_Title,"VPDF",
80 MUIA_Window_Width, MUIV_Window_Width_Screen(50),
81 MUIA_Window_Height, MUIV_Window_Height_Screen(50),
82 MUIA_Window_AppWindow, TRUE,
83 MUIA_Window_RootObject,
84 grpRoot = VGroup,
85 MUIA_Background, MUII_RegisterBack,
86 MUIA_Frame, MUIV_Frame_Register,
87 MUIA_Group_PageMode, TRUE,
88 Child, grpTitles = NewObject(getTitleClass(), NULL,
89 #if !defined(__AROS__)
90 // FIXME: AROS
91 MUIA_Title_Closable, TRUE,
92 MUIA_Title_Sortable, TRUE,
93 #endif
94 TAG_DONE),
95 End,
96 TAG_MORE, INITTAGS);
98 if (obj != NULL)
100 GETDATA;
101 memset(data, 0, sizeof(struct Data));
102 data->id = GetTagData(MUIA_VPDFWindow_ID, 0, INITTAGS);
103 data->grpRoot = grpRoot;
104 data->grpTitles = grpTitles;
106 /* setup window id */
108 DoMethod(obj, MUIM_Notify, MUIA_AppMessage, MUIV_EveryTime, MUIV_Notify_Application, 3, MUIM_VPDF_HandleAppMessage, data->id, MUIV_TriggerValue);
109 DoMethod(grpRoot, MUIM_Notify, MUIA_Group_ActivePage, MUIV_EveryTime, obj, 2, MUIM_VPDFWindow_UpdateActive, MUIV_TriggerValue);
111 DoMethod(obj, MUIM_VPDFWindow_CreateTab);
114 return (ULONG)obj;
117 DEFMMETHOD(VPDFWindow_CreateTab)
119 GETDATA;
121 /* place empty object into tab */
123 DoMethod(data->grpRoot, MUIM_Group_InitChange);
124 DoMethod(data->grpTitles, MUIM_Group_InitChange);
125 DoMethod(data->grpTitles, MUIM_Family_AddTail, VPDFTitleButtonObject,
126 Child, TextObject,
127 MUIA_Text_Contents, LOCSTR( MSG_NOFILE ) ,
128 // MUIA_Text_Copy, TRUE, // acc. to MUI4 autodocs that's already the default
129 End,
130 End);
132 DoMethod(data->grpRoot, MUIM_Family_AddTail, VGroup,
133 MUIA_UserData, TRUE, // move it to subclass
134 Child, RectangleObject,
135 End,
136 End);
138 DoMethod(data->grpRoot, MUIM_Group_ExitChange);
139 DoMethod(data->grpTitles, MUIM_Group_ExitChange);
141 /* return index of new group member */
143 set(data->grpRoot, MUIA_Group_ActivePage, xget(data->grpRoot, MUIA_Family_ChildCount) - 1);
144 return xget(data->grpRoot, MUIA_Family_ChildCount) - 1;
147 DEFMMETHOD(VPDFWindow_OpenFile)
149 GETDATA;
150 char *filename = msg->filename;
151 int mode = msg->mode;
152 int newtab;
154 if (mode == MUIV_VPDFWindow_OpenFile_CurrentTabIfEmpty)
156 int tabind = xget(data->grpRoot, MUIA_Group_ActivePage);
157 Object *tcontents = (Object*)DoMethod(data->grpRoot, MUIM_Family_GetChild, tabind + 1); // +1 for title object
158 if (xget(tcontents, MUIA_UserData) == TRUE) // marker for empty page. fix
159 newtab = FALSE;
160 else
161 newtab = TRUE;
163 else if (mode == MUIV_VPDFWindow_OpenFile_NewTab)
165 newtab = TRUE;
167 else // if (MUIV_VPDFWindow_OpenFile_CurrentTab)
169 newtab = FALSE;
173 Object *document;
174 int tabind, i;
175 Object *contents, *tcontents;
177 if (newtab)
178 tabind = DoMethod(obj, MUIM_VPDFWindow_CreateTab);
179 else
180 tabind = xget(data->grpRoot, MUIA_Group_ActivePage);
182 /* setup new label */
184 tcontents = (Object*)DoMethod(data->grpTitles, MUIM_Family_GetChild, tabind);
185 set(tcontents, MUIA_Text_Contents, filename);
187 /* dispose old contents and add new browser */
189 contents = (Object*)DoMethod(data->grpRoot, MUIM_Family_GetChild, tabind + 1); /* +1 for title object */
190 DoMethod(contents, MUIM_Group_InitChange);
191 tcontents = (Object*)DoMethod(contents, MUIM_Family_GetChild, 0);
193 DoMethod(contents, OM_REMMEMBER, tcontents);
194 MUI_DisposeObject(tcontents);
196 tcontents = VGroup,
197 Child, document = DocumentViewObject,
198 MUIA_DocumentView_Renderer, xget(_app(obj), MUIA_VPDF_Renderer),
199 MUIA_DocumentView_FileName, filename,
200 MUIA_DocumentView_Layout, xget(_app(obj), MUIA_VPDFSettings_Layout),
201 MUIA_DocumentView_Outline, xget(_app(obj), MUIA_VPDFSettings_Outline),
202 MUIA_DocumentLayout_Scaling, xget(_app(obj), MUIA_VPDFSettings_Scaling),
203 End,
204 End;
206 if (tcontents != NULL)
207 DoMethod(contents, MUIM_Family_AddTail, tcontents);
209 set(contents, MUIA_UserData, FALSE); // mark as not empty
210 DoMethod(contents, MUIM_Group_ExitChange);
211 DoMethod(obj, MUIM_VPDFWindow_UpdateActive, tabind);
212 return tcontents != NULL;
216 DEFMMETHOD(VPDFWindow_UpdateActive)
218 GETDATA;
220 Object *group = (Object*)DoMethod(data->grpRoot, MUIM_Family_GetChild, msg->active + 1); /* +1 for title object */
221 group = (Object*)DoMethod(group, MUIM_Family_GetChild, 0);
223 if (group != NULL)
225 void *doc = xget(group, MUIA_DocumentView_PDFDocument);
226 struct pdfAttribute *attr = NULL;
228 if (doc != NULL)
229 attr = pdfGetAttr(doc, PDFATTR_TITLE);
231 if (attr != NULL && attr->value.s != NULL)
232 snprintf(data->title, sizeof(data->title), "VPDF [%s]", attr->value.s);
233 else if (xget(group, MUIA_DocumentView_FileName))
234 snprintf(data->title, sizeof(data->title), "VPDF [%s]", FilePart((char*)xget(group, MUIA_DocumentView_FileName)));
235 else
236 snprintf(data->title, sizeof(data->title), "VPDF");
238 set(obj, MUIA_Window_Title, data->title);
239 pdfFreeAttr(doc, attr);
242 return 0;
245 DEFMMETHOD(VPDFWindow_DetachView)
247 GETDATA;
249 if (msg->tabind == MUIV_VPDFWindow_DetachView_Active)
251 int tabind = xget(data->grpRoot, MUIA_Group_ActivePage);
252 Object *tcontents = (Object*)DoMethod(data->grpTitles, MUIM_Family_GetChild, tabind);
253 Object *contents = (Object*)DoMethod(data->grpRoot, MUIM_Family_GetChild, tabind + 1);
255 DoMethod(data->grpTitles, MUIM_Group_InitChange);
256 DoMethod(data->grpRoot, MUIM_Group_InitChange);
258 DoMethod(data->grpTitles, OM_REMMEMBER, tcontents);
259 DoMethod(data->grpRoot, OM_REMMEMBER, contents);
261 DoMethod(data->grpTitles, MUIM_Group_ExitChange);
262 DoMethod(data->grpRoot, MUIM_Group_ExitChange);
264 msg->docview = contents;
265 msg->title = tcontents;
267 /* hmm, should we really close the window here? */
269 if (xget(data->grpTitles, MUIA_Family_ChildCount) == 0)
270 DoMethod(_app(data->grpTitles), MUIM_Application_PushMethod, obj, 3, MUIM_Set, MUIA_Window_CloseRequest, TRUE);
272 return TRUE;
275 return FALSE;
278 DEFMMETHOD(VPDFWindow_AttachView)
280 GETDATA;
281 int tabind = msg->tabind;
282 int newtab;
283 Object *contents, *tcontents;
285 if (tabind == MUIV_VPDFWindow_OpenFile_CurrentTabIfEmpty)
287 int tabind = xget(data->grpRoot, MUIA_Group_ActivePage);
288 Object *tcontents = (Object*)DoMethod(data->grpRoot, MUIM_Family_GetChild, tabind + 1); // +1 for title object
289 if (xget(tcontents, MUIA_UserData) == TRUE) // marker for empty page. fix
290 newtab = FALSE;
291 else
292 newtab = TRUE;
294 else if (tabind == MUIV_VPDFWindow_OpenFile_NewTab)
296 newtab = TRUE;
298 else // if (MUIV_VPDFWindow_OpenFile_CurrentTab)
300 newtab = FALSE;
303 if (newtab)
304 tabind = DoMethod(obj, MUIM_VPDFWindow_CreateTab);
305 else
306 tabind = xget(data->grpRoot, MUIA_Group_ActivePage);
308 /* dispose old title and put whole new object inplace */
310 tcontents = (Object*)DoMethod(data->grpTitles, MUIM_Family_GetChild, tabind);
311 DoMethod(data->grpTitles, MUIM_Group_InitChange);
312 DoMethod(data->grpTitles, OM_REMMEMBER, tcontents);
313 MUI_DisposeObject(tcontents);
314 DoMethod(data->grpTitles, MUIM_Family_AddTail, msg->title);
315 DoMethod(data->grpTitles, MUIM_Group_ExitChange);
317 /* dispose old contents and add new browser */
319 contents = (Object*)DoMethod(data->grpRoot, MUIM_Family_GetChild, tabind + 1); /* +1 for title object */
320 DoMethod(data->grpRoot, MUIM_Group_InitChange);
321 DoMethod(data->grpRoot, OM_REMMEMBER, contents);
322 MUI_DisposeObject(contents);
323 DoMethod(data->grpRoot, MUIM_Family_AddTail, msg->docview);
324 DoMethod(data->grpRoot, MUIM_Group_ExitChange);
326 DoMethod(obj, MUIM_VPDFWindow_UpdateActive, tabind);
327 return TRUE;
330 DEFGET
332 GETDATA;
334 switch (msg->opg_AttrID)
336 case MUIA_VPDFWindow_ID:
337 *(ULONG*)msg->opg_Storage = data->id;
338 return TRUE;
340 case MUIA_VPDFWindow_PDFDocument:
342 void *doc = NULL;
343 Object *group = (Object*)DoMethod(data->grpRoot, MUIM_Family_GetChild, xget(data->grpRoot, MUIA_Group_ActivePage) + 1); /* +1 for title object */
344 group = (Object*)DoMethod(group, MUIM_Family_GetChild, 0);
346 if (group != NULL)
347 doc = xget(group, MUIA_DocumentView_PDFDocument);
349 *(ULONG*)msg->opg_Storage = (ULONG)doc;
350 return TRUE;
353 case MUIA_VPDFWindow_ActiveDocumentView:
355 Object *group = (Object*)DoMethod(data->grpRoot, MUIM_Family_GetChild, xget(data->grpRoot, MUIA_Group_ActivePage) + 1); /* +1 for title object */
356 group = (Object*)DoMethod(group, MUIM_Family_GetChild, 0);
358 if (group != NULL)
360 *(ULONG*)msg->opg_Storage = (ULONG)group;
361 return TRUE;
363 break;
367 return DOSUPER;
370 BEGINMTABLE
371 DECNEW
372 DECGET
373 DECMMETHOD(VPDFWindow_CreateTab)
374 DECMMETHOD(VPDFWindow_OpenFile)
375 DECMMETHOD(VPDFWindow_UpdateActive)
376 DECMMETHOD(VPDFWindow_DetachView)
377 DECMMETHOD(VPDFWindow_AttachView)
379 ENDMTABLE
381 DECSUBCLASS_NC(MUIC_Window, VPDFWindowClass)
383 struct VPDFTitleButton_Data
385 Object *obj;
388 BEGINMTABLE2(VPDFTitleButtonClass)
389 ENDMTABLE
391 DECSUBCLASS2_NC(MUIC_Group, VPDFTitleButtonClass)